[英]str.format() option is not working
This code was taken from the tutorial: 此代码取自教程:
def main():
stri = "Hello, {person}"
stri.format(person="James")
print(stri) #prints "Hello, {person}"
Why the format()
is not working? 为什么
format()
不起作用?
It does work. 它确实有效。 You were just not assigning the format to a variable, and then just printing the original string.
您只是没有将格式分配给变量,然后只打印原始字符串。 See example below:
见下面的例子:
>>> s = 'hello, {person}'
>>> s
'hello, {person}'
>>> s.format(person='james')
'hello, james' # your format works
>>> print s # but you did not assign it
hello, {person} # original `s`
>>> x = s.format(person='james') # now assign it
>>> print x
hello, james # works!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.