Does Python do variable interpolation similar to “string #{var}” in Ruby?

后端 未结 9 1187
盖世英雄少女心
盖世英雄少女心 2020-12-08 06:37

In Python, it is tedious to write:

print \"foo is\" + bar + \'.\'

Can I do something like this in Python?

print \"foo is #{ba

9条回答
  •  没有蜡笔的小新
    2020-12-08 07:15

    I prefer this approach because you don't have to repeat yourself by referencing the variable twice:

    alpha = 123
    print 'The answer is {alpha}'.format(**locals())
    

提交回复
热议问题