Skip to main content
 首页 » 编程设计

python之模板字符串 python 2.5 错误

2025年05月04日20unruledboy

#!/usr/bin/python 
 
from string import Template 
 
s = Template('$x, go home $x') 
s.substitute(x='lee') 
 
print s 

我得到的错误是

<string.Template object at 0x81abdcc> 

我正在寻找的期望结果是:lee,回家 lee

请您参考如下方法:

您需要查看substitute 的返回值。它为您提供执行了替换的字符串。

print s.substitute(x='lee') 

模板对象本身(s)没有改变。这使您能够使用同一模板对象执行多个替换。