简体   繁体   中英

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM