标签:pre 格式化字符串 格式化 使用 python2 code dict lis sda
Python2.6 开始,新增了一种格式化字符串的函数 str.format()。使用起来简单方便,不会遇到使用%时候格式的选择问题。
>>> "yesday is {}, today is {}".format("saturday", "sunday")
‘yesday is saturday, today is sunday‘
>>>
>>> "yesday is {0}, today is {1}, good day is {0}".format("saturday", "sunday")
‘yesday is saturday, today is sunday, good day is saturday‘
>>>
#!/usr/bin/python
s = "I am a {name}, and study {subject}".format(name="coder", subject="ES")
print s
output:
I am a coder, and study ES
# dict
d = {"name": "coder", "subject": "VUE"}
s = "I am a {name}, and study {subject}".format(**d)
print s
output:
I am a coder, and study VUE
# list
l = ["coder", "big data"]
s = "I am a {0}, and study {1}".format(*l)
print s
output:
I am a coder, and study big data
标签:pre 格式化字符串 格式化 使用 python2 code dict lis sda
原文地址:https://www.cnblogs.com/lanyangsh/p/9865007.html