美文网首页
Python字符串格式化

Python字符串格式化

作者: slords | 来源:发表于2018-08-27 21:38 被阅读0次

python 1.0+ (printf-style-string-formatting)

name = 'world'
print('hello %s' % name)
# 'hello world'

python 2.6+ (PEP 3101 -- Advanced String Formatting)

action = 'hello'
name = 'guest'
language = 'python'
print('{0} {name}, welcome to {language} world'.format(
    action, name=name, language=language))
# hello guest, welcome to python world

python 3.6+ (PEP 498 -- Literal String Interpolation)

action = 'hello'
name = 'guest'
language = 'python'
print(f'{action} {name}, welcome to {language} world')
# hello guest, welcome to python world

相关文章

网友评论

      本文标题:Python字符串格式化

      本文链接:https://www.haomeiwen.com/subject/cffxwftx.html