开发者

String formatting: string specifier in a string constant

Is there a way to insert a string in a string constant/variable that contains a string specifier?

Example:

temp_body = 'Hello %s, please visit %s to confirm your registration.'
bod开发者_如何学Goy = temp_body % (name, url)

But this raises a TypeError.


Usually it is the way strings are generated e.g. msg template will be loaded from db or some file and things inserted in between, what is url and name in your case?

This works on my machine

>>> temp_body = 'Hello %s, please visit %s to confirm your registration.'
>>> temp_body%("anurag", "stackoverflow")
'Hello anurag, please visit stackoverflow to confirm your registration.'

Also try if str(name), str(url) works , ost probably it won't and try to fix that problem instead.


Works on my machine(TM).

Are you sure that name and url really are strings? What do you get when you do

>>> type(name), type(url)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜