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 | Explanations |
---|---|
%d, %i | Convert to decimal integer/ integer |
%o | Convert to octal integer |
%x, %X | Convert to hexadecimal integer |
%e | Convert to floating point (lowercase e) |
%E | Convert to floating point (uppercase e) |
%f, %F | Convert to decimal floating point |
%g | Smart selection using %f or %e format |
%G | Smart selection using %F or %E format |
%c | Formatting characters and their ASCII codes |
%r | Use the repr() function to convert an expression to a string |
%s | 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.
Example:
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.
Example:
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)
The output is:
a(10): 889977.
a(5):889977.
name(30): Free Learning Platform.
name(20):Free Learning Platform.
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 data.
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:
Flag | Explanations |
---|---|
- | Specify left alignment |
+ | The output numbers must always be signed; integers are +, and negative numbers are-. |
0 | Indicates that the extra width is filled with 0 instead of spaces. |
A few things to take note:
For example:
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)
The output is:
a(08):00088866.
a(+8):+88866.
b(-+0):+240.500000.
c(-10):Jason.
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.nf
.nf %
While as:
m is the minimum width,
n is the output accuracy,
. must exist.
For example:
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)
The output is:
2.164
002.164
+02.164
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises