How to convert numbers to strings in python: 1. Use formatted strings, statements such as "tt=322 tem='%d' %tt"; 2. Implement through the "str(5)" method Convert.
Recommended: "python tutorial"
Convert numbers to strings
Method 1:
Use format string:
tt=322 tem='%d' %tt
tem is the string converted by tt
Commonly used format string:
%d integer
%f%F floating point number
%e%E scientific notation
Abbreviation of %g%G e and %f/%E and %F
%% Output%
Format operator auxiliary instructions
Symbol function
* Define width or decimal point precision
- Used for left alignment
Display a plus sign ( ) before a positive number
# Display a zero ('0') before an octal number, in '0x' or '0X' is displayed in front of the hexadecimal number (depending on whether 'x' or 'X' is used)
0 The displayed number is filled with '0' instead of the default space
% '%%' outputs a single '%'
(var) mapping variable (dictionary parameter)
m.n m is the minimum total width displayed, n is after the decimal point Number of digits (if available)
Example:
'%f' % 1234.567890 输出:'1234.567890' '%.2f' % 1234.567890 输出:'1234.57'
Method 2:
str(5)
Example:
>>> '10'+str(4) '104' >>> str='hello' >>> '10'+str(4) Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> '10'+str(4) TypeError: 'str' object is not callable >>>
The above is the detailed content of How to convert numbers to strings in python. For more information, please follow other related articles on the PHP Chinese website!