Home > Backend Development > Python Tutorial > What does format in python mean?

What does format in python mean?

(*-*)浩
Release: 2019-07-03 11:11:15
Original
18557 people have browsed it

format is a new method for formatting strings in python2.6. Compared with the old version of the % format method, it has many advantages.

What does format in python mean?

1. There is no need to worry about the data type. In the % method, %s can only replace the string type (recommended learning: Python video tutorial )

2. A single parameter can be output multiple times, and the order of parameters can be different

3. The filling method is very flexible, and the alignment method is very powerful

4.Officially recommended method, the % method will be eliminated in later versions

An example of format

print 'hello {0}'.format('world')
Copy after login

Fill the string by position

print'hello {0} i am {1}'.format('Kevin','Tom')                  
hello Kevin i am Tom
print'hello {} i am {}'.format('Kevin','Tom')                    
hello Kevin i am Tom
print'hello {0} i am {1} . myname is {0}'.format('Kevin','Tom')
my name is Kevin
Copy after login

foramt will fill the parameters into the string in positional order. The first parameter is 0, then 1...

You can also enter no number, so it will be filled in order

The same parameter can be filled multiple times. This is where format is more advanced than %

Filling through key

print 'hello {name1}  i am {name2}'.format(name1='Kevin',name2='Tom')                  
hello Kevin i am Tom
Copy after login

More Python related technical articles, Please visit the Python Tutorial column to learn!

The above is the detailed content of What does format in python mean?. 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