Python字符串格式化

一、使用格式化符来格式化字符串:

Python支持的所有格式化符

格式化符 意义
'd' 返回要格式化对象的十进制表示,如果可以
'i' 返回要格式化对象的十进制表示,如果可以
'o' 返回要格式化对象的八进制表示,如果可以
'u' 同格式化符'd'
'x' 返回要格式化对象的十六进制表示,如果可以【如果要求的前导,使用'0x'】
'X' 返回要格式化对象的十六进制表示,如果可以【如果要求的前导,使用'0X'】
'e' 返回要格式化对象的浮点的科学计数的表示,如果可以【使用'e'】
'E' 返回要格式化对象的浮点的科学计数的表示,如果可以【使用'E'】
'f' 返回要格式化对象的浮点数表示,如果可以
'F' 返回要格式化对象的浮点数表示,如果可以
'g' 使用小写字母科学计数格式,如果指数小于-4或不少于精度,否则返回十进制格式。
'G' 使用大写字母科学计数格式,如果指数小于-4或不少于精度,否则返回十进制格式。
'c' 对0-255之间的整数返回其对应的ASCII码字符(其它整数报错),或者返回单个字符本身
'r' 返回要格式化对象的__repr__()方法的返回值
's' 返回要格式化对象的__str__()方法的返回值
'%' 即每两个%显示一个

1.%s格式化符:将要格式化的值表中对应位置的元素,格式化为字符串,如果值元素不是字符串,将自动调用该元素的__str__(),以得到其字符串表示。

[1]%ns决定对齐与填充:n为整数;当n>0时,左填充,当n<0时,右填充。

 1 >>> for i,x in enumerate(('one','two','three','four','five','six')):
 2     print '%5s = %d'%(x,i)
 3 
 4     
 5   one = 0
 6   two = 1
 7 three = 2
 8  four = 3
 9  five = 4
10   six = 5
11 >>> 

[2]%.ns决定对字符串的截取:n为正整数

 1 >>> for i,x in enumerate(('one','two','three','four','five','six')):
 2     print '%.3s = %d'%(x,i)
 3 
 4     
 5 one = 0
 6 two = 1
 7 thr = 2
 8 fou = 3
 9 fiv = 4
10 six = 5
11 >>> 

2.%d格式化符:将任何Python对象转化为整数,如果转化失败,则报错。

[1]%nd:决定对齐与填充。n为整数;当n>0时,左填充,当n<0时,右填充。

1 >>> print '%3d'%1.52
2   1
3 >>> 

[2]%0nd:以数字0而不是默认的空格来作填充。

1 >>> print '%03d'%1.52
2 001
3 >>> 

[3]%+d:数字被转化后将保留正负号

1 >>> print '%+d'%1.52
2 +1
3 >>> print '%+d'%-1.52
4 -1
5 >>> print '%+3d'%1.52
6  +1
7 >>> print '%+03d'%1.52
8 +01
9 >>> 

3.%r格式化符:将任何Python对象转化成repr字符串,自动调用Python对象的__repr__()

[1]%nr:决定对齐与填充。n为整数;当n>0时,左填充,当n<0时,右填充。

1 >>> print 'Names = #%50r#'%(['C','CPP','CSharp','Java','Python'])
2 Names = #          ['C', 'CPP', 'CSharp', 'Java', 'Python']#
3 >>> print 'Names = #%-50r#'%(['C','CPP','CSharp','Java','Python'])
4 Names = #['C', 'CPP', 'CSharp', 'Java', 'Python']          #
5 >>> 

[2]%.nr:决定截取字符数。n为正整数

1 >>> print 'Names = #%.10r#'%(['C','CPP','CSharp','Java','Python'])
2 Names = #['C', 'CPP#
3 >>> 

4.%o与%x格式化符:将十进制数值格式化为八进制与十六进制字符串;当有'#'时,格式化后的字符串带前导

 1 >>> '%x'%20
 2 '14'
 3 >>> '%#x'%20
 4 '0x14'
 5 >>> '%#X'%20    #使用X作前缀,如果使用#要求了前缀
 6 '0X14'
 7 >>> '%o'%10
 8 '12'
 9 >>> '%#o'%10
10 '012'
11 >>> 

注意:如果要格式化的数值是浮点数,将先取整,再格式化

1 >>> '%o'%13.2
2 '15'
3 >>> '%x'%20.3232
4 '14'
5 >>> '%x'%20.5232
6 '14'
7 >>> 

5.%f格式化符:以浮点数格式化数值。

[1]%nf:格式化数字后填充到固定宽度;当n>0时,左填充,当n<0时,右填充。【若不指定小数位数时,最小宽度8位,如'0.000000'】

1 >>> '%20f'%32
2 '           32.000000'
3 >>> '%-20f'%32
4 '32.000000           '
5 >>> 

[2]%.nf:格式化数字后的小数位数;n>=0且为整数

1 >>> '%.3f'%0.3245
2 '0.325'
3 >>> '%.0f'%0.3233
4 '0'
5 >>> '%.8f'%0.3245
6 '0.32450000'
7 >>> 

6.%e格式化符:以浮点科学计数的方式转化数值,即10的指数幂形式

[1]%ne:将格式化后的数字字符串进行填充到指定宽度;当n>0时,左填充,当n<0时,右填充。【如不指定小数位数,最小宽度是12位,如'0.000000e+00'】

1 >>> '%15e'%8283489323232
2 '   8.283489e+12'
3 >>> '%-15e'%8283489323232
4 '8.283489e+12   '
5 >>> 

[2]%.ne:指定小数位数;n>=0且为整数。

1 >>> '%.10e'%82834
2 '8.2834000000e+04'
3 >>> '%.2e'%8283489323232
4 '8.28e+12'
5 >>> 

7.%c格式化符:将0-255之间的数字转化成对应的ASCII码字符,也可以转化单个字符,不过基本不做什么,直接返回

1 >>> '%c'%32
2 ' '
3 >>> print '%c'%'A'
4 A
5 >>> print '%c'%8
6 
7 >>> 

【上面的第五行的语句在这里显示不了结果,请在IDLE或者shell中执行,就可看到结果】

8.其它格式化符,因为不同情况下有不同表现,所以不常用,如果你清楚其工作方式,自己选用吧。

二、使用str.format(*args, **kwargs)方法来格式化字符串

1.序数占位符:

1 >>> '{0} is our new {1}, welcome {0}.'.format('Tom','teacher')
2 'Tom is our new teacher, welcome Tom.'
3 >>> args=('Tom','teacher')
4 >>> '{0} is our new {1}, welcome {0}.'.format(*args)
5 'Tom is our new teacher, welcome Tom.'
6 >>> 

2.命名占位符:

1 >>> '{name} is a lovly girl, she likes {fruit} and {food}.'.format(name='Lily',fruit='apple',food='rice')
2 'Lily is a lovly girl, she likes apple and rice.'
3 >>> kws=dict(name='Lily',fruit='apple',food='rice')
4 >>> kws
5 {'food': 'rice', 'fruit': 'apple', 'name': 'Lily'}
6 >>> '{name} is a lovly girl, she likes {fruit} and {food}.'.format(**kws)
7 'Lily is a lovly girl, she likes apple and rice.'
8 >>> 

3.以上两种可以混用,且序数占位符的序数可以不给出,即使用空占位符形式,它会自动从零起开始给空占位符编序号,但切记空占位符不能与编了序数的占位符混用。

 1 >>> '{} is a boy, {} is {age}.'.format('Tom','he',age=12)
 2 'Tom is a boy, he is 12.'
 3 >>> numbers=('one','two','three','four','five','six','seven')
 4 >>> 'I love the numbers: {3},{5}.'.format(0,*numbers) #0只是占个位,方便选择第3、5个元素
 5 'I love the numbers: three,five.'
 6 >>> '{0} is a boy, {} is {age}.'.format('Tom','he',age=12)
 7 
 8 Traceback (most recent call last):
 9   File "<pyshell#11>", line 1, in <module>
10     '{0} is a boy, {} is {age}.'.format('Tom','he',age=12)
11 ValueError: cannot switch from manual field specification to automatic field numbering
12 >>> 

4.支持内部格式语法,即{:xxx}这种语法

format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::= <any character>
align       ::= "<" | ">" | "=" | "^"
sign        ::= "+" | "-" | " "
width       ::= integer
precision   ::= integer
type        ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"

[1]align填充之对齐标记:

align char Meaning
'<' 在右边填充
'>' 在左边填充
'=' 填充时强制在正负号与数字之间进行填充,只支持对数字的填充
'^' 在两边填充
 1 >>> for align, text in zip('<^>', ['left', 'center', 'right']):
 2     print 'fill = align = %r, text = %r, output:'%(align,text)
 3     print '{0:{fill}{align}16}'.format(text, fill=align, align=align)
 4     print
 5 
 6     
 7 fill = align = '<', text = 'left', output:
 8 left<<<<<<<<<<<<
 9 
10 fill = align = '^', text = 'center', output:
11 ^^^^^center^^^^^
12 
13 fill = align = '>', text = 'right', output:
14 >>>>>>>>>>>right
15 
16 >>> def sy():
17     a,b=12345,8794
18     print '{: =6}'.format(a)
19     print '{: =+6}'.format(b)
20     print '-'*6
21     print '{: =6}'.format(a+b)
22 
23     
24 >>> sy()
25  12345
26 + 8794
27 ------
28  21139
29 >>> 

 [2]sign数字之前导标记:

sign char Meaning
'+' 强制对数字使用正负号
'-' 仅对负数使用前导负号(这是默认的行为)
' ' 对正数使用一个' '作前导,负数仍以'-'为前导

【参见上例】

[3]#被用来与type为'b'/'o'/'x'/'X'合作,对整数进行格式化,格式化后数字前分别以'0b'/'0o'/'0x'/'0X'为前导

1 >>> #str.format支持数值向二进制格式化,是使用%进行字符串格式化所无法做到的
2 >>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)
3 'int: 42;  hex: 2a;  oct: 52;  bin: 101010'
4 >>> #如上str.format支持对同一Python对象进行多次不同格式化
5 >>> #使用'#'时,将会自动给各种格式化结果加上前导,如果有前导
6 >>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
7 'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'
8 >>>

[4]','被用来对数字整数部分进行千分位分隔

1 >>> '{:,}'.format(1234567890)
2 '1,234,567,890'
3 >>> 

[5].precision对于数字对象,用来指定数字的小数位数,如果有小数;对于非数字对象,用来指定最终返回的格式化字符的最大长度,即格式化完成后,以这个precision参数对结果进行截取

 1 >>> '{:.2f}'.format(3.235)
 2 '3.23'
 3 >>> '{:.2}'.format('hello')
 4 'he'
 5 >>> '%.2f'%3.235
 6 '3.23'
 7 >>> '%.2f'%3.2353
 8 '3.24'
 9 >>> '{:.2f}'.format(3.2351)
10 '3.24'
11 >>> 

[6]type用来指定格式化时的转换方式

<1>当要格式化的Python对象是str对象时

Type Meaning
's' 字符串格式,这是默认格式
None 与's'相同,即原数据是str类型,且不指定type时,将采用默认的's'格式
1 >>> print '{{0:}}=={{0:s}}\n{0:}=={0:s}'.format('hello')
2 {0:}=={0:s}
3 hello==hello
4 >>> 

由上面的例子可见,在str.format()方法的调用中,两个'{'显示出一个,两个'}'也显示出一个

<2>当要格式化的Python对象是integer对象时

Type Meaning
'b' 将数值转换成二进制字符串
'c' 将数值转换成对应的unicode字符,比%c强多了,它支持0-255之外的字符
'd' 十进制数值
'o' 八进制数值
'x' 十六进制数值,如果要求前缀,则使用0x
'X' 十六进制数值,如果要求前缀,则使用0X
'n' 同'd'
None 在原数值为整数时,如果又没有指定type,则此时使用'd'作为type

【参见例子2.4.3】

<3>当要格式化的Python对象是浮点数对象时

Type Meaning
'e' 科学计数,使用字母'e',默认精度6
'E' 科学计数,使用字母'E',默认精度6
'f' 浮点数,默认精度6
'F' 浮点数,默认精度6
'g' 很难用,弃之不说
'G' 同'g'
'n' 同'g'
'%' 对原数值乘以100后,加上'%’作后缀
None 同'g'

【因为'g'的用法难以掌握,且规则复杂,而'e'/'f'/'%'已经足够使用了,所以这里就弃之不说了,有兴趣的可以自行研究】

1 >>> '{:f}'.format(32.3863)
2 '32.386300'
3 >>> '{:e}'.format(3738)
4 '3.738000e+03'
5 >>> '{:.2%}'.format(100/365.0)
6 '27.40%'
7 >>> 

5.使用要格式化对象的属性来进行格式化

1 >>> from collections import namedtuple
2 >>> Point=namedtuple('Point','X Y')
3 >>> p=Point(3,7)
4 >>> p
5 Point(X=3, Y=7)
6 >>> 'Point({p.X}, {p.Y})'.format(p=p)
7 'Point(3, 7)'
8 >>> 

6.使用要格式化对象的项来进行格式化

1 >>> person=('Bill',23,'male')
2 >>> "Name: {0[0]}, Age: {0[1]}, Sex: {0[2]}".format(person)
3 'Name: Bill, Age: 23, Sex: male'
4 >>> 

7.使用{!r}和{!s}取得%r和%s的效果

 1 >>> '{!r},{!s}'.format('hello','world')
 2 "'hello',world"
 3 >>> #下面的无法成功,表明其它格式化符并不支持这种搞法
 4 >>> '{!d}'.format(32)
 5 
 6 Traceback (most recent call last):
 7   File "<pyshell#19>", line 1, in <module>
 8     '{!d}'.format(32)
 9 ValueError: Unknown conversion specifier d
10 >>> 

8.如果要格式化的Python对象拥有自己的格式化字符,那么也可以用这个对象的格式化符进行format

1 >>> import datetime
2 >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
3 >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
4 '2010-07-04 12:15:58'

三、str.format的基础——Format String Syntax,其定义如下:

1 replacement_field ::=  "{" [field_name] ["!" conversion] [":" format_spec] "}"
2 field_name        ::=  arg_name ("." attribute_name | "[" element_index "]")*
3 arg_name          ::=  [identifier | integer]
4 attribute_name    ::=  identifier
5 element_index     ::=  integer | index_string
6 index_string      ::=  <any source character except "]"> +
7 conversion        ::=  "r" | "s"
8 format_spec       ::=  <described in the previous section>

1.通过第1、2行,可知上面的2.5与2.6的语法基础,而arg_name既可以是成员访问形式的标识符也可以是索引访问形式的序数,所以

[1]2.5的例子可以改为:

1 >>> from collections import namedtuple
2 >>> Point=namedtuple('Point','X Y')
3 >>> p=Point(3,7)
4 >>> p
5 Point(X=3, Y=7)
6 >>> 'Point({0.X}, {0.Y})'.format(p)
7 'Point(3, 7)'
8 >>> 

[2]2.6的例子可以改为:

1 >>> person=('Bill',23,'male')
2 >>> "Name: {person[0]}, Age: {person[1]}, Sex: {person[2]}".format(person=person)
3 'Name: Bill, Age: 23, Sex: male'
4 >>> 

2.通过第5行,可以知道element_index既可以是整数索引也可以是字符串索引,所以也可以使用字典的字符串键来索引字典的值

1 >>> person={'name':'Bill','age':23,'sex':'male'}
2 >>> "Name: {0[name]}, Age: {0[age]}, Sex: {0[sex]}".format(person)
3 'Name: Bill, Age: 23, Sex: male'
4 >>> 

【要注意的是,占位符中的字符串索引必须不带字符串定界符】

3.通过第1、7行,可知占位符中只允许出现!r和!s两种,其它都不支持;而它的前后也可以有其它内容,来进行更细节的格式化控制;每个!r或!s占位符分别对对应的要被格式化的值调用obj.__repr__()或obj.__str__()

1 >>> 'The variable value is {0!r},{0!r:.3}'.format('hello')
2 "The variable value is 'hello','he"
3 >>> 

四、应用技巧

1.多级格式化:使用内部自转义,'%%'=>'%','{{'=>'{','}}'=>'}'

1 >>> '%s, %%s' % 'Hello' % 'Gates'
2 'Hello, Gates'
3 >>> '{greet},{{name}}'.format(greet='Hello').format(name='Gates')
4 'Hello,Gates'
5 >>> '%%%ds'%10%'fuck'
6 '      fuck'
7 >>> 

 

posted @ 2015-05-18 00:49  nutix  阅读(22336)  评论(1编辑  收藏  举报