开发者

String formatting: string specifier in a string constant

开发者 https://www.devze.com 2022-12-25 18:47 出处:网络
Is there a way to insert a string in a string constant/variable that contains a string specifier? Example:

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

精彩评论

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