The print () function uses string modulo operator that begin with % to format and produce various
types of data. See the table below for details.
String Modulo Operator
Convert to decimal integer/ integer
Convert to octal integer
Convert to hexadecimal integer
Convert to floating point (lowercase e)
Convert to floating point (uppercase e)
Convert to decimal floating point
Smart selection using %f or %e format
Smart selection using %F or %E format
Formatting characters and their ASCII codes
Use the repr() function to convert an expression to a string
Use the str() function to convert an expression to a string
String modulo operator is just a placeholder, and it will be replaced by the value of the
variables, constants, numbers, strings, addition, subtraction, multiplication and division.
age = 22
print ("Jason is% d years old!" % age)
The output is:
Jason is 22 years old!
The formatting string in the above example contains string modulo operator, %d which is a
placeholder and eventually be replaced by the value of age variable. The % in the middle of sentence
acts as delimiter, which is preceded by a formatted string and followed by the output result.
The formatting string can also contain multiple string modulo operator. At this time,
multiple expressions must be provided in order to replace the corresponding string modulo operators.
The multiple expressions must be enclosed in parentheses (), you can find the following example:
name = "Jason"
age = 22
place = "Secret Garden"
print ("% s is% d years old, he stays at % s."% (name, age, place))
The output is:
Jason is 22 years old, he stays at Secret Garden.
In short, the number of placeholder shall same as number of expressions which enclosed in
parenthesis () and situated in the end of formatting string.
How to specify minimum output width in Python?
% 10d indicates that the integer width of the output is at least 10;
% 20s indicates that the output string width is at least 20.
a = 889977
print("a(10):%10d." % a)
print("a(5):%5d." % a)
name = "Free Learning Platform"
print("name(30):%35s." % name)
print("name(20):%20s." % name)
It can be found from the results that for integers and strings, when the actual width of the data
is less than the specified width, it will be filled in with spaces on the left; when the actual width of
the data is greater than the specified width, it will be filled in according to the actual width of the
How to adjust the Alignment in Python?
By default, the data printed by print () is always right justified. That is, when the data is
not wide enough, the data is always generate result on the right, and spaces are added on the left to
reach the specified width. Python allows adding a flag before the minimum width to change the alignment.
The flags supported by Python are as follows:
Specify left alignment
The output numbers must always be signed; integers are +, and negative numbers are-.
Indicates that the extra width is filled with 0 instead of spaces.
A few things to take note:
For integers, when left-aligned is specified, 0 on the right side has no effect,
because it will cause the changes of integer value.
For decimals, the above three flags can exist at the same time.
For strings, you can only use the (-) flag, because other symbols have no meaning
for strings, and add in 0 changes the value of the string.
a = 88866
# % 08d means the minimum width is 8
print ("a (08):% 08d"% a)
#% + 8d means the minimum width is 8 with a symbol
print ("a (+8):% + 8d"% a)
b = 240.5
#%-+ 010d means the minimum width is 10, left-aligned, with symbols
print ("b (-+ 10):%-+ 010f"% b)
c = "Jason"
#% -10s means minimum width is 10, left justified
print ("c (-10):%-10s."% c)
For decimals (floating point numbers), print () also allows you to specify the number of digits
after the decimal point, that is, the precision of the decimals.
The precision value needs to be placed after the minimum width, separated by dots in the middle;
you can also write the precision without writing the minimum width. The specific format is as follows:
m is the minimum width,
n is the output accuracy,
. must exist.
e = 2.164192853
# Minimum width is 7, 3 digits are reserved after the decimal point
print ("% 7.3f"% e)
# The minimum width is 7, 3 digits are reserved after the decimal point and 0 are filled in the left.
print ("% 07.3f"% e)
# The minimum width is 7 and 3 digits are reserved after the decimal point and 0 are filled in the left which a (+) symbol.
print ("% + 07.3f"% e)