format is a new method for formatting strings in python2.6. Compared with the old version of the % format method, it has many advantages.
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')
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
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
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!