Python高手之路【六】python基础之字符串格式化

Python的字符串格式化有两种方式: 百分号方式、format方式,百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。[PEP-3101],This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing ‘%’ string formatting operator.,注:Python中百分号格式化是不存在自动将整数转换成二进制表示的方式,常用格式化:,常用格式化:,更多格式化操作:https://docs.python.org/3/library/string.html,tpl = “i am %s” % “poe”

tpl = “i am %s age %d” % (“poe”, 18)

tpl = “i am %(name)s age %(age)d” % {“name”: “poe”, “age”: 18}

tpl = “percent %.2f” % 99.97623

tpl = “i am %(pp).2f” % {“pp”: 123.425556, }

tpl = “i am %.2f %%” % {“pp”: 123.425556, },tpl = “i am {}, age {}, {}”.format(“seven”, 18, ‘alex’)

tpl = “i am {}, age {}, {}”.format(*[“seven”, 18, ‘alex’])

tpl = “i am {0}, age {1}, really {0}”.format(“seven”, 18)

tpl = “i am {0}, age {1}, really {0}”.format(*[“seven”, 18])

tpl = “i am {name}, age {age}, really {name}”.format(name=”seven”, age=18)

tpl = “i am {name}, age {age}, really {name}”.format(**{“name”: “seven”, “age”: 18})

tpl = “i am {0[0]}, age {0[1]}, really {0[2]}”.format([1, 2, 3], [11, 22, 33])

tpl = “i am {:s}, age {:d}, money {:f}”.format(“seven”, 18, 88888.1)

tpl = “i am {:s}, age {:d}”.format(*[“seven”, 18])

tpl = “i am {name:s}, age {age:d}”.format(name=”seven”, age=18)

tpl = “i am {name:s}, age {age:d}”.format(**{“name”: “seven”, “age”: 18})

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

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

tpl = “numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}”.format(15)

tpl = “numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}”.format(num=15),

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!10210454@qq.com
2. 本站不保证所提供所有下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理,有奖励!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有RB奖励和额外RMB收入!

磊宇堂正在使用的服务器 维护管理由磊宇云服务器提供支持

磊宇堂 » Python高手之路【六】python基础之字符串格式化

提供最优质的资源集合

立即查看 了解详情