簡體   English   中英

str.format()選項不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM