Python String Format
Python String Format
Here, p0, p1,... are positional arguments and, k0, k1,... are keyword arguments with
values v0, v1,... respectively.
And, template is a mixture of format codes with placeholders for the arguments.
The format() reads the type of arguments passed to it and formats it according to the
TUTORIAL EXAMPLES BUILT-IN FUNCTIONS
format codes de ned in the string.
The string "Hello {0}, your balance is {1:9.3f}" is the template string. This contains the
format codes for formatting.
The curly braces are just placeholders for the arguments to be placed. In the above
example, {0} is placeholder for "Adam" and {1:9.3f} is placeholder for 230.2346 .
Since the template string references format() arguments as {0} and {1}, the arguments are
positional arguments. They both can also be referenced without the numbers as {} and
Python internally converts them to numbers.
Internally,
Since "Adam" is the 0th argument, it is placed in place of {0}. Since, {0} doesn't
contain any other format codes, it doesn't perform any other operations.
However, it is not the case for 1st argument 230.2346 . Here, {1:9.3f} places
230.2346 in its place and performs the operation 9.3f.
f speci es the format is dealing with a oat number. If not correctly speci ed, it will
give out an error.
The part before the "." (9) speci es the minimum width/padding the number
(230.2346) can take. In this case, 230.2346 is allotted a minimum of 9 places including
the ".".
If no alignment option is speci ed, it is aligned to the right of the remaining spaces.
(For strings, it is aligned to the left.)
The part after the "." (3) truncates the decimal part (2346) upto the given number. In
this case, 2346 is truncated after 3 places.
Remaining numbers (46) is rounded o outputting 235.
https://www.programiz.com/python-programming/methods/string/format 2/15
3/20/2018 Python String format()
TUTORIAL
For EXAMPLES
keyword BUILT-IN FUNCTIONS
arguments
We've used the same example from above to show the di erence between keyword and
positional arguments.
Here, instead of just the parameters, we've used a key-value for the parameters. Namely,
name="Adam" and blc=230.2346 .
Since, these parameters are referenced by their keys as {name} and {blc:9.3f} , they are
known as keyword or named arguments.
Internally,
The placeholder {name} is replaced by the value of name - "Adam". Since, it doesn't
contain any other format codes, "Adam" is placed.
For the argument blc=230.2346, the placeholder {blc:9.3f} is replaced by the value
230.2346. But before replacing it, like previous example, it performs 9.3f operation on
it.
This outputs 230.235. The decimal part is truncated after 3 places and remaining
digits are rounded o . Likewise, the total width is assigned 9 leaving two spaces to
the left.
https://www.programiz.com/python-programming/methods/string/format 3/15
3/20/2018 Python String format()
6
# keyword arguments
7
TUTORIAL
8
EXAMPLES
print("Hello {name}, yourBUILT-IN FUNCTIONS
balance is {blc}.".format(name="Adam", blc=230.2346))
9
# mixed arguments
10
print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346))
11
Run
Powered by DataCamp
When you run the program, the output will be same for all:
Note: In case of mixed arguments, keyword arguments has to always follow positional
arguments.
Type Meaning
d Decimal integer
b Binary format
o Octal format
n Same as 'd'. Except it uses current locale setting for number separator
https://www.programiz.com/python-programming/methods/string/format 4/15
3/20/2018 Python String format()
Type Meaning
TUTORIAL EXAMPLES BUILT-IN FUNCTIONS
E Exponential notation (uppercase E)
Run
Powered by DataCamp
Run
Powered by DataCamp
1 2
1 2 3 4
1 2 . 2 3 5
0 0 0 1 2
0 0 1 2 . 2 3 5
Here,
in the rst statement, {:5d} takes an integer argument and assigns a minimum
width of 5. Since, no alignment is speci ed, it is aligned to the right.
In the second statement, you can see the width (2) is less than the number (1234), so
it doesn't take any space to the left but also doesn't truncate the number.
Unlike integers, oats has both integer and decimal parts. And, the mininum width
de ned to the number is for both parts as a whole including ".".
In the third statement, {:8.3f} truncates the decimal part into 3 places rounding o
the last 2 digits. And, the number, now 12.235, takes a width of 8 as a whole leaving
2 places to the left.
If you want to ll the remaining places with zero, placing a zero before the format
speci er does this. It works both for integers and oats: {:05d} and {:08.3f} .
https://www.programiz.com/python-programming/methods/string/format 6/15
3/20/2018 Python String format()
Powered by DataCamp
+12.230000 -12.230000
12.230000 -12.230000
1 2 . 2 3 0 0 0 0 - 1 2 . 2 3 0 0 0 0
The operators < , ^ , > and = are used for alignment when assigned a certain width to
the numbers.
Type Meaning
https://www.programiz.com/python-programming/methods/string/format 7/15
3/20/2018 Python String format()
TUTORIAL
Run
EXAMPLES BUILT-IN FUNCTIONS
Powered by DataCamp
1 2
1 2 . 2 3 5
1 2 0 0 0
- 1 2 . 2 3 5
Note: Left alignment lled with zeros for integer numbers can cause problems as the 3rd
example which returns 12000, rather than 12.
Run
Powered by DataCamp
c a t
c a t
c a t
* c a t *
https://www.programiz.com/python-programming/methods/string/format 8/15
3/20/2018 Python String format()
Run
Powered by DataCamp
c a t
c a t
c a t
Run
https://www.programiz.com/python-programming/methods/string/format 9/15
3/20/2018 Python String format()
Powered by DataCamp
TUTORIAL EXAMPLES BUILT-IN FUNCTIONS
When you run this program, the output will be:
Inside the template string, Person's name and age are accessed using .name and .age
respectively.
Run
Powered by DataCamp
Inside the template string, person's name and age are accessed using [name] and [age]
respectively.
https://www.programiz.com/python-programming/methods/string/format 10/15
3/20/2018 Python String format()
4 # format age
5 print("{name}'s
TUTORIAL age is: {age}".format(**person))
EXAMPLES BUILT-IN FUNCTIONS
Run
Powered by DataCamp
Run
Powered by DataCamp
* * c a t * *
1 2 3 . 2 4
Here,
https://www.programiz.com/python-programming/methods/string/format 11/15
3/20/2018 Python String format()
format() internally calls __format__() for datetime, while format() accesses the attributes of
the complex number.
You can easily override the __format__() method of any object for custom formatting.
Run
Powered by DataCamp
https://www.programiz.com/python-programming/methods/string/format 12/15
3/20/2018 Python String format()
For datetime:
Current datetime is passed as a positional argument to the format() method.
And, internally using __format__() method, format() accesses the year, month, day,
hour, minutes and seconds.
For complex numbers:
1+2j is internally converted to a ComplexNumber object.
Then accessing its attributes real and imag , the number is formatted.
Overriding __format__():
Like datetime, you can override your own __format__() method for custom formatting
which returns age when accessed as {:age}
You can also use object's __str__() and __repr__() functionality with shorthand notations
using format().
Like __format__(), you can easily override object's __str__() and __repr_() methods.
Run
Powered by DataCamp
https://www.programiz.com/python-programming/methods/string/format 13/15
3/20/2018 Python String format()
Want to learn more Python Data Science? Head over to DataCamp and try
forFUNCTIONS
TUTORIAL EXAMPLES BUILT-IN
their free Python Tutorial
String Methods
Python String capitalize()
https://www.programiz.com/python-programming/methods/string/format 14/15
3/20/2018 Python String format()
Subscribe
ABOUT
CONTACT
ADVERTISE
C PROGRAMMING
C++ PROGRAMMING
R PROGRAMMING
https://www.programiz.com/python-programming/methods/string/format 15/15