Home > Backend Development > Python Tutorial > How to convert numbers to strings in python

How to convert numbers to strings in python

藏色散人
Release: 2020-10-14 16:06:17
Original
75701 people have browsed it

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
Copy after login

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 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'
Copy after login

Method 2:

str(5)
Copy after login

Example:

>>> '10'+str(4)
'104'
>>> str='hello'
>>> '10'+str(4)
Traceback (most recent call last):
 File "<pyshell#25>", line 1, in <module>
  &#39;10&#39;+str(4)
TypeError: &#39;str&#39; object is not callable
>>>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template