小编Buf*_*ter的帖子

Python中命名的字符串格式参数

我正在使用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}"

python python-2.7

5
推荐指数
1
解决办法
686
查看次数

标签 统计

python ×1

python-2.7 ×1