Tutorial Study Image

Python format()

The format() function in python helps to replace or substitute the string with placeholders. The placeholder is defined by using curly brackets( { } ). Finally, the formatted string is returned as output.

template.format(p0, p1, ..., k0=v0, k1=v1, ...) #Where p&k are either a list of values or combination of both 

format() Parameters:

The format() function can take any number of parameters, but it will be split into two sections.

1.Positional parameters - list of parameters inside curly braces {index} and can be accessed with an index of the parameter.

2.Keyword parameters - list of parameters inside curly braces {key} that can be accessed with key of parameter like type key=value

And here the 'template' is a combination of format codes with placeholders for the arguments.

Parameter Description Required / Optional
p0, p1,... positional arguments Required
k0, k1,...v0, v1,... keyword arguments with values Required

format() Return Value

Here return value of this method is a string. We can also format numbers using the format specifiers like below

d -    Decimal integer
c -    Corresponding Unicode character
b -    Binary format
o -    Octal format
x -    Hexadecimal format (lower case)
X-    Hexadecimal format (upper case)
n -   Same as 'd' except it uses current locale setting for number separator
e -   Exponential notation. (lowercase e)
E -   Exponential notation (uppercase E)
f  -   Displays fixed-point number (Default: 6)
F -   Same as 'f' except displays 'inf' as 'INF' and 'nan' as 'NAN'
g -   General format which rounds a number to p significant digits. (Default precision: 6)
G -   Same as 'g' except switches to 'E' if the number is large.
% -   Percentage. Multiples by 100 and puts % at the end.

Number formatting with alignment is also possible using

< -   Left aligned 
^ -   Center aligned 
> -   Right aligned
= -    Forces the signed (+) (-) to the leftmost position

Input Return Value
string formatted string

Examples of format() method in Python

Example 1:How format()works for default, positional and keyword arguments?


# default arguments
print("Hii {}, your balance is {}.".format("Jhon", 500.322))

# positional arguments
print("Hii {0}, your balance is {1}.".format("Jhon", 500.322))

# keyword arguments
print("Hii {name}, your balance is {blc}.".format(name="Jhon", blc=500.322))

# mixed arguments
print("Hii  {0}, your balance is {blc}.".format("Jhon", blc=500.322))

 

Output:


Hello Jhon, your balance is 500.322.
Hello Jhon, your balance is 500.322.
Hello Jhon, your balance is 500.322.
Hello Jhon, your balance is 500.322.

Example 2: How to do number formatting?


# integer arguments
print("The number is:{:d}".format(567))

# float arguments
print("The float number is:{:f}".format(567.5634278))

# octal, binary and hexadecimal format
print("bin: {0:b}, oct: {0:o}, hex: {0:x}".format(12))
 

Output:


The number is: 567
The number is: 567.5634278
bin: 1100, oct: 14, hex: c

Example 3: How to do string formatting with padding and alignment?


# string padding with left alignment
print("{:5}".format("abc"))

# string padding with right alignment
print("{:>5}".format("abc"))

# string padding with center alignment
print("{:^5}".format("abc"))

# string padding with center alignment
# and '*' padding character
print("{:*^5}".format("abc"))
 

Output:


abc  
  abc
 abc 
*abc*

Example 4: How to truncating strings with format()?


# truncating strings to 3 letters
print("{:.3}".format("caterpillar"))

# truncating strings to 3 letters
# and padding
print("{:5.3}".format("caterpillar"))

# truncating strings to 3 letters,
# padding and center alignment
print("{:^5.3}".format("caterpillar"))
 

Output:


cat
cat  
 cat