Python基础-字符串格式化

xiaoxiao2021-02-28  27

Format 方式:

常用格式化:

1 tpl = "i am {}, age {}, {}".format("seven", 18, 'alex') 2 3 #必须一一对应,否则会报错 4 tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) 5 6 7 tpl = "i am {0}, age {1}, really {0}".format("seven", 18) 8 9 10 tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18]) 11 12 13 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 14 15 16 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 17 ** 代表传字典 18 19 tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) 20 21 22 tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) 23 s 代表字符串 d 代表整数 24 25 tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 26 * 代表列表 27 28 tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) 29 30 31 tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) 32 33 34 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 35 2进制 8进制 10进制 x与X: 16进制 %:百分比 36 37 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 38 39 40 tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) 41 42 43 tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15) 示例:

ps1:

1 tpl = "i am {0}, age {1}, really {2}".format("seven", '18','alex') 2 print(tpl)

执行结果:

1 i am seven, age 18, really alex

 

ps2:

1 tpl = "i am {2}, age {1}, really {0}".format("seven", '18','alex') 2 print(tpl)

执行结果:

1 i am alex, age 18, really seven

 

ps3: 传字典的方式

1 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 2 print(tpl)

执行结果:

1 i am seven, age 18, really seven

 

ps4: ** 传字典的方式 (跟ps3的方式是一样的)

1 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 2 print(tpl)

执行结果:

1 i am seven, age 18, really seven

 

ps5:  * 代表:列表

1 tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 2 print(tpl)

执行结果:

1 i am seven, age 18

 

ps6:

1 tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18] 2 print(tpl)

执行结果:

1 i am seven, age 18

 

ps7:

1 l=["seven", 18] 2 tpl = "i am {:s}, age {:d}".format('seven',18) 3 print(tpl)

执行结果:

1 i am seven, age 18

 

ps8:   2进制 8进制 10进制 x与X: 16进制 %:百分比

1 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2) 2 print(tpl)

执行结果:

1 numbers: 1111,17,15,f,F, 1587.623000%,2 百分号方式:

常用格式化:

1 tpl = "i am %s" % "alex" 2 3 tpl = "i am %s age %d" % ("alex", 18) 4 5 tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} 6 7 tpl = "percent %.2f" % 99.97623 #打印浮点数 8 9 tpl = "i am %(pp).2f" % {"pp": 123.425556, } 10 11 tpl = "i am %.2f %%" % {"pp": 123.425556, } #打开百分比

 

示例:

1、%s 可以字符串拼接

1 msg='i am %s my hobby is %s' % ('lhf','alex') 2 print(msg)

执行结果:

1 i am lhf my hobby is alex

 

2、%s 可以按收任何类型( 数字对应字符串)

1 msg='i am %s my hobby is %s' % ('lhf',1) 2 print(msg)

执行结果:

1 i am lhf my hobby is 1

 

3、%s 可以字符串接收列表

1 msg='i am %s my hobby is %s' % ('lhf',[1,2]) 2 print(msg)

执行结果:

1 i am lhf my hobby is [1, 2]

 

4、%s传字符串或任何值, %d只能接收数字

1 name='lhf' 2 age=19 3 msg='i am %s my hobby is %d' % (name,age) #age可以用%d or %s,但用%s程序可读性差 4 print(msg)

执行结果:

1 i am lhf my hobby is 19

 

5、%d 只能接收数字类型,不能接收列表

1 # %d 只能按收数字类型 2 msg='i am %s my hobby is %d' % ('lhf',1) 3 print(msg)

执行结果:

1 i am lhf my hobby is 1

 

6、%d 不能接收列表类型,会报错

1 msg='i am %s my hobby is %d' % ('lhf',[1,2]) 2 print(msg)

执行结果:

1 Traceback (most recent call last): 2 File "D:/python/day5/format_type.py", line 14, in <module> 3 msg='i am %s my hobby is %d' % ('lhf',[1,2]) 4 TypeError: %d format: a number is required, not list

 

7、打印浮点数

1 tpl = "percent %.2f" % 99.976234444444444444 2 print(tpl)

执行结果:

1 percent 99.98

 

8、打印百分比

1 tpl = 'percent %.2f %%' % 99.976234444444444444 2 print(tpl)

执行结果:

1 percent 99.98 %

 

9、左边打印空格

1 msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'} 2 print(msg)

执行结果:

1 i am lhf my hobby is alex

 

10、打印空格加颜色(黄色)

1 msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'} 2 print(msg)

执行结果:

1 i am lhf my hobby is alex

 

19、不用格式化的方式

1 print('root','x','0','0',sep=':') 2 print('root'+':'+'x'+':'+'0','0')

执行结果:

1 root:x:0:0 2 root:x:0 0
转载请注明原文地址: https://www.6miu.com/read-250158.html

最新回复(0)