Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Creating dynamic strings (http://www.programmingforums.org/showthread.php?t=8713)

Sane Mar 4th, 2006 5:46 PM

Creating dynamic strings
 
I'm wondering if there's a way to create a string that will allow % plus arguments to follow. So basically splitting up

:

print "this is a string %s"%("this is another string")

In to two parts, allowing the first to be dynamic.

This is an example of how you would do it with your own function.

:

def dyn_string(x, d):
    for key in d.keys():
        x = x.replace("<%s>"%key, d[key])
    return x 

x = """One day, <noun> decided to post a <adjective> question on <noun2>.
Even though <noun>'s <adverb> for a better alternative, <noun> is <adjective2> with what <noun>'s got working.
"""

print dyn_string(x, {"noun":"Sane", "adjective":"stupid", "noun2":"PFO", "adverb":"hoping", "adjective2":"happy"})


Is there a built in method for the same result? Idealy I'd like it to work like so:

:

x = "this is a string %s"

print x%"this is another string"

But obviously that doesn't work. EDIT HOLY CRAP. I feel so stupid. It actually does work... god... /EDIT

I want this so I can take a dynamic template and stick in specific values appropriately.


All times are GMT -5. The time now is 9:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC