replace %(word) in a string

Max M maxm at mxm.dk
Wed Sep 17 04:38:42 EDT 2003


Maxime Biais wrote:

> Hello,
> 
> I heard that is possible to replace %(word) in a string with a dictionary
> containing { "word" : "replaced word" } with a function or a builtin from
> the python library.
> 
> For example with the string: "hello, %(word1) %(word2) !" and the
> dictionary: {"word1" : "python", "word2" : "lovers"} the final string
> would be: "hello python lovers"


You are almost there.

"hello, %(word1)s %(word2)s !" % {"word1":"python", "word2":"lovers"}

 >>>'hello, python lovers !'

regards Max M





More information about the Python-list mailing list