为什么python有很多方法可以在字符串内插变量?

时间:2019-12-17 11:37:30

标签: python

根据此网络文章:https://www.programiz.com/python-programming/string-interpolation

可以将f函数与胡子符号{}结合使用来对变量进行插值。第二个可以使用%-formatter,第三个可以在现有字符串上使用.format()函数,再加上小胡子卷发{},最后还有一个Template类允许您像这样在字符串中插入变量:

name = 'world'
program ='python'
new = Template('Hello $name! This is $program.')
print(new.substitute(name= name,program=program))

为什么这么多不同的方法可以做基本相同的事情?是因为其中大多数方法都是旧版python附带的,现在已经过时了?

谢谢

0 个答案:

没有答案