[英]str.format() option is not working
此代碼取自教程:
def main():
stri = "Hello, {person}"
stri.format(person="James")
print(stri) #prints "Hello, {person}"
為什么format()
不起作用?
它確實有效。 您只是沒有將格式分配給變量,然后只打印原始字符串。 見下面的例子:
>>> 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.