我正在使用Python 2.x,并尝试使用命名参数来理解字符串格式的逻辑.我明白:
"{} and {}".format(10, 20)打印'10 and 20'.
以类似的方式'{name} and {state}'.format(name='X', state='Y')打印X and Y
但为什么这不起作用?
my_string = "Hi! My name is {name}. I live in {state}"
my_string.format(name='Xi', state='Xo')
print(my_string)
Run Code Online (Sandbox Code Playgroud)
它打印 "Hi! My name is {name}. I live in {state}"