Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 4th, 2006, 4:46 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:04 AM.

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