The Format method

The format method

Sometimes we may want to construct strings from other information as well. This is where the format() method is useful.

age = 21
name = 'Abhishek'

print('{0} was {1} years old when he wrote this course'.format(name, age))
print('My Name is {0} '.format(name))

Output

Abhishek was 21 years old when he wrote this course
My Name is Abhishek

How It Works

A string can use certain specifications and the format method can be called to substitute those specifications with corresponding arguments to the format method.

Observe in the first usage where we use {0} and this corresponds to the variable name which is the first argument to the format method. Similarly, the second specification is {1} corresponding to age which is the second argument to the format method. Note that Python starts counting from 0 which means that first position is at index 0, second position is at index 1, and so on.

Notice that we could have achieved the same using string concatenation:

name + ’ is ’ + str(age) + ’ years old’

  1. but that is much ugly and can lead to error.
  2. Second, the conversion to string would be done automatically by the format method instead of the explicit conversion to strings needed in this case.
  3. Third, when using the format method, we can change the message without having to deal with the variables used and vice-versa.

Also note that the numbers are optional, so you could have also written as:

which will give the same exact output as the previous program.

age = 21
name = 'Abhishek'

print('{} was {} years old when he wrote this course'.format(name, age))
print('My Name is {}'.format(name))

Output

Abhishek was 21 years old when he wrote this course
My Name is Abhishek

We can also name the parameters:

age = 21
name = 'Abhishek'

print('{name} was {age} years old when he wrote this course'.format(name=name, age=age))
print('My Name is {name}'.format(name=name))

Output

Abhishek was 21 years old when he wrote this course
My Name is Abhishek

which will give the same exact output as the previous program.

Python 3.6 introduced a shorter way to do named parameters, called “f-strings”:

age = 21
name = 'Abhishek'

print(f'{name} was {age} years old when he wrote this course')  # notice the 'f' before the string it tells python that it is a f-string 
print(f'My Name is {name} ')  # notice the 'f' before the string

which will give the same exact output as the previous program.

What Python does in the f-string method is that it substitutes(or put) each argument value into the place of the specification. Some other of format method example :

# decimal (.) precision of 3 for float '0.333'
print('{0:.3f}'.format(1.0/3))
# fill with underscores (_) with the text centered
# (^) to 11 width '___hello___'
print('{0:_^11}'.format('hello'))
# keyword-based 'Abhishek wrote this course'
print('{name} wrote {course}'.format(name='Abhishek', course='this course'))

Output

0.333
___hello___
Abhishek wrote this course

Since we are discussing formatting, note that print always ends with an invisible “new line” character (\n) so that repeated calls to print will all print on a separate line each. To prevent this newline character from being printed, you can specify that it should end with a blank.

Whatever value we passed in the end parameter it displayed in the output.

print('a', end=' ') # we pass single space in the end parameter
print('b', end='     ') #here we pass five spaces in the end paarmeter
print('c', end='')

Output

a b     c
print('a', end=' ')
print('b', end='++++') # we can pass any value and it will displayed in the output
print('c')

Output

a b++++c

That's it for this article Hope you like it...