tags:

views:

88

answers:

2

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.'
body = temp_body % (name, url)

But this raises a TypeError.

+1  A: 

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)
Tim Pietzcker
The problem was with my original string not returning a string with valid specifiers. Sorry to trouble you..
hyn
+1  A: 

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.

Anurag Uniyal
I'm sorry, you're right, I was using the wrong string.
hyn