如何使用python中str.format() 方法?
发布时间:2024-10-09 12:11:48
方法1、str.format()
通过字符串中的花括号{}识别替换字段replacement field,完成字符串的格式化。
2、基本语法
通过 {} 和 : 取代以前的 %,也就是说,接收的参数不需要限制数据类型。
3、基本用法
>>>print'Wearethe{}whosay"{}!"'.format('knights','Ni')
Wearetheknightswhosay"Ni!"4、具体使用实例
'{:b}'.format(11)
'{:d}'.format(11)
'{:o}'.format(11)
'{:x}'.format(11)
'{:#x}'.format(11)
'{:#X}'.format(11)输出
1011 11 13 b 0xb 0XB
对齐文本并指定特定的宽度 以上是python中的strr.format()方法介绍,希望对你有所帮助~
下一篇 python rjust函数用法
