views:

120

answers:

4

This is probably a very simple question for some, but it has me stumped. Can you use variables within python's triple-quotes?

In the following example, how do use variables in the text:

wash_clothes = 'tuesdays'
clean_dishes = 'never'

mystring =""" I like to wash clothes on %wash_clothes
I like to clean dishes %clean_dishes
"""

print(mystring)

I would like it to result in:

 I like to wash clothes on tuesdays
     I like to clean dishes never

If not what is the best way to handle large chunks of text where you need a couple variables, and there is a ton of text and special characters?

+5  A: 

One of the ways :

>>> mystring =""" I like to wash clothes on %s
... I like to clean dishes %s
... """
>>> wash_clothes = 'tuesdays'
>>> clean_dishes = 'never'
>>> 
>>> print mystring % (wash_clothes, clean_dishes)
 I like to wash clothes on tuesdays
I like to clean dishes never

Also look at string formatting

pyfunc
That is perfect. Thanks so much! That is exactly what I was looking for! I don't exactly get how python knows which %s to replace with which variable, though. Does it look at the order the variables appear in the following: print mystring % (wash_clothes, clean_dishes)
XL
@XL : You can take a look at the other formatting technique for which I had provided the link but the example. The example was provided by NullUserException. You can use both the techniques.
pyfunc
A: 

Yes. I believe this will work.

do_stuff = "Tuesday"

mystring = """I like to do stuff on %(tue)s""" % {'tue': do_stuff}

EDIT: forgot an 's' in the format specifier.

jonesy
If you're going to mark down an answer, please say why, so that we all can learn from the mistake.
jonesy
+5  A: 

The preferred way of doing this is using str.format() rather than the method using %:

This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.

Example:

wash_clothes = 'tuesdays'
clean_dishes = 'never'

mystring =""" I like to wash clothes on {0}
I like to clean dishes {1}
"""

print mystring.format(wash_clothes, clean_dishes)
NullUserException
+2  A: 

I think the simplest way is str.format() as others have said.

However, I thought I'd mention that Python has a string.Template class starting in Python2.4.

Here's an example from the docs.

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'

One of the reasons I like this is the use of a mapping instead of positional arguments.

chauncey
That is an interesting approach. I really don't like the positional arguments either as it may become tricky if you have a really large number of variables. The only question that I have is whether this method is worthwhile if you have massive amounts of formatted text and special characters in the template string?
XL
you can use positional arguments with `format`. `d = dict(foo=1, bar=2); "{foo} {bar}".format(**d)`
aaronasterling
@AaronMcSmooth: I think they're called "keyword arguments", not "positional arguments", when what's in the braces is a name rather than a number, but your point -- that `format()` can do what `string` `Template` does using names -- is correct (although the syntax is a little different).
martineau
@martineau. yup. that was definitely a tipo.
aaronasterling