Home  >  Article  >  Backend Development  >  What does format mean in python?

What does format mean in python?

(*-*)浩
(*-*)浩Original
2019-06-25 10:33:1116760browse

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 mean in python?

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 is very powerful

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

An example of format

print 'hello {0}'.format('world')

Output:

hello world

Specific use case:

#通过位置
print '{0},{1}'.format('chuhao',20)
 
print '{},{}'.format('chuhao',20)
 
print '{1},{0},{1}'.format('chuhao',20)
 
#通过关键字参数
print '{name},{age}'.format(age=18,name='chuhao')
 
class Person:
    def __init__(self,name,age):
        self.name = name
        self.age = age
 
    def __str__(self):
        return 'This guy is {self.name},is {self.age} old'.format(self=self)
 
print str(Person('chuhao',18))
 
#通过映射 list
a_list = ['chuhao',20,'china']
print 'my name is {0[0]},from {0[2]},age is {0[1]}'.format(a_list)
#my name is chuhao,from china,age is 20
 
#通过映射 dict
b_dict = {'name':'chuhao','age':20,'province':'shanxi'}
print 'my name is {name}, age is {age},from {province}'.format(**b_dict)
#my name is chuhao, age is 20,from shanxi
 
#填充与对齐
print '{:>8}'.format('189')
#     189
print '{:0>8}'.format('189')
#00000189
print '{:a>8}'.format('189')
#aaaaa189
 
#精度与类型f
#保留两位小数
print '{:.2f}'.format(321.33345)
#321.33
 
#用来做金额的千位分隔符
print '{:,}'.format(1234567890)
#1,234,567,890
 
#其他类型 主要就是进制了,b、d、o、x分别是二进制、十进制、八进制、十六进制。
 
print '{:b}'.format(18) #二进制 10010
print '{:d}'.format(18) #十进制 18
print '{:o}'.format(18) #八进制 22
print '{:x}'.format(18) #十六进制12

For more Python-related technical articles, please visit the Python Tutorial column to learn!

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