普通用法
'{} {}'.format('hello', 'world')
'{1} {0}'.format('world', 'hello')
数字的格式化输出
x = 1234.5678
format(x, '.2f')
format(x, '>10.2f')
format(x, '^10.2f')
format(x, ',')
format(x, '^10,.2f')
'{:^10.2f}'.format(x)
根据变量名格式化
website_name = '百度'
website_url = 'www.baidu.com'
f"{website_name} {website_url}"
"{website_name} {website_url}".format(website_name=website_name, website_url=website_url)
利用字典格式化输出
website_dic = {'website': '百度', 'url': 'www.baidu.com'}
'{website} {url}'.format(**website_dic)
填充和对齐操作
format('hello', '^20')
format('hello', '>20')
format('hello', '<20')
'{:^20}'.format('hello')
'{:>20}'.format('hello')
'{:<20}'.format('hello')
format('hello', '=^20')
format('hello', '=>20')
format('hello', '=<20')
进制转换
x = 1234
bin(x)
oct(x)
hex(x)
format(x, 'b')
format(x, 'o')
format(x, 'x')