Home  >  Article  >  Backend Development  >  How to convert numbers to strings in python

How to convert numbers to strings in python

藏色散人
藏色散人Original
2020-10-13 17:51:4675534browse

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.

How to convert numbers to strings in python

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

40248cdb8ba3b8fe238e2ffa5e6e3cd8 Display a space 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 "", line 1, in 
  '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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn