字符串格式化
%格式化: C 语言风格的 sprintf 形式, 用%占位
print("八进制:%o" % 222)
print("整数:%d, %d, %d" % (1, 22.33, 0.25))
print("十六进制:%x" % 12)
print("浮点数保留两位小数:%.2f" % 3.1415926)
print("%r, %r, %r, %r, %r, %r, %r" % ("abc", 25, 3.1415926, [1, 2, 3], (1, 2, 3), {1, 2, 3}, {'a': 1, 'b': 2, 'c': 3}))
print("%.3s" % ("abcdefg"))
print("%.*s" % (4, "abcdefg"))
'''
八进制:336
整数:1, 22, 0
十六进制:c
浮点数保留两位小数:3.14
'abc', 25, 3.1415926, [1, 2, 3], (1, 2, 3), {1, 2, 3}, {'a': 1, 'b': 2, 'c': 3}
abc
abcd
'''
'This is %s' % name
'This is %s' % (name
,)
str.format()格式化:函数把字符串单层一个模版,通过传入的参数进行格式化,用{}占位
print('This is {}{}'.format("Python", "!"))
print('This is {0}{1} {0} is esay{1}'.format("Python", "!"))
print('This is {name}{symbol}'.format(name
="Python", symbol
="!"))
print('This is {name}{symbol}'.format(**{"name": "Python", "symbol": "!"}))
print("整数:{:d}".format(123))
print("浮点数保留两位小数:{:.2f}".format(3.1415926))
print("字符串:{:s}".format("abcdefg"))
print("八进制:{:o}".format(222))
print("十六进制:{:x}".format(12))
print("编号组合数值格式化:{0:d}, {1:s}".format(123, "abcdefg"))
print("关键字结合数值格式化:{num:d}, {string:s}".format(num
= 123, string
= "abcdefg"))
print("百分数:{:.2%}".format(3/7))
print("金钱分隔符:{:,}".format(123456789))
print("输出大括号:{}为{{1, 2, 3}}".format("集合"))
'''
This is Python!
This is Python! Python is esay!
This is Python!
This is Python!
整数:123
浮点数保留两位小数:3.14
字符串:abcdefg
八进制:336
十六进制:c
编号组合数值格式化:123, abcdefg
关键字结合数值格式化:123, abcdefg
百分数:42.86%
金钱分隔符:123,456,789
输出大括号:集合为{1, 2, 3}
'''
f-string格式化:Python 3.6 新加入的方法,风格简洁、易读、速度快
name
, age
= "lucy", 20
print(f
"she is {name}, {age} years old.")
info
= {'name': "lucy", 'age': 20}
print(f
"she is {info['name']}, {info['age']} years old.")
print(f
"she is {name}, {age: .2f} years old.")
print(f
"she is {name:<10}, {age:<3} years old.")
name1
, age1
= "Liming", 3
print(f
"she is {name1:<10}, {age1:<3} years old.")
name2
, age2
= "Lee", 101
print(f
"she is {name2:<10}, {age2:<3} years old.")
print(f
"整数:{123:d}")
print(f
"浮点数保留两位小数:{3.1415926:.2f}")
print(f
"字符串:{'abcdefg':s}")
print(f
"八进制:{222:o}")
print(f
"十六进制:{12:x}")
print(f
"百分数:{(3/7):.2%}")
print(f
"金钱分隔符:{123456789:,}")
'''
she is lucy, 20 years old.
she is lucy, 20 years old.
she is lucy, 20.00 years old.
she is lucy , 20 years old.
she is Liming , 3 years old.
she is Lee , 101 years old.
整数:123
浮点数保留两位小数:3.14
字符串:abcdefg
八进制:336
十六进制:c
百分数:42.86%
金钱分隔符:123,456,789
'''
相对于%,format()的优点:
(1).在%方法中%s只能替代字符串类型,而.format()方法不用考虑数据类型问题;(2).单个参数可以多次输出,参数顺序可以不相同;(3).填充方式十分灵活,对齐方式十分强大;(4).可以使用列表、元组格式化,可以使用字典格式化(传入用**解构);
import timeit
def add():
status
= 200
body
= 'hello world'
return 'Status: ' + str(status
) + '\r\n' + body
+ '\r\n'
def precent_style():
status
= 200
body
= 'hello world'
return 'Status: %s\r\n%s\r\n' % (status
, body
)
def format_style1():
status
= 200
body
= 'hello world'
return 'Status: {}\r\n{}\r\n'.format(status
, body
)
def format_style2():
status
= 200
body
= 'hello world'
return 'Status: {status}\r\n{body}\r\n'.format(status
=status
, body
=body
)
def f_string():
status
= 200
body
= 'hello world'
return f
'Status: {status}\r\n{body}\r\n'
print('add: '+ str(min(timeit
.repeat
(lambda: add
()))))
print('precent_style: '+ str(min(timeit
.repeat
(lambda: precent_style
()))))
print('format_style1: '+ str(min(timeit
.repeat
(lambda: format_style1
()))))
print('format_style2: '+ str(min(timeit
.repeat
(lambda: format_style2
()))))
print('f_string: '+ str(min(timeit
.repeat
(lambda: f_string
()))))
'''
add: 1.0201557999999995
precent_style: 0.7589769999999998
format_style1: 1.2661715999999998
format_style2: 1.2782307000000017
f_string: 0.5236783999999979
'''