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:
JavaScript
x
9
1
wash_clothes = 'tuesdays'
2
clean_dishes = 'never'
3
4
mystring =""" I like to wash clothes on %wash_clothes
5
I like to clean dishes %clean_dishes
6
"""
7
8
print(mystring)
9
I would like it to result in:
JavaScript
1
3
1
I like to wash clothes on tuesdays
2
I like to clean dishes never
3
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?
Advertisement
Answer
One of the ways in Python 2 :
JavaScript
1
10
10
1
>>> mystring =""" I like to wash clothes on %s
2
I like to clean dishes %s
3
"""
4
>>> wash_clothes = 'tuesdays'
5
>>> clean_dishes = 'never'
6
>>>
7
>>> print mystring % (wash_clothes, clean_dishes)
8
I like to wash clothes on tuesdays
9
I like to clean dishes never
10
Also look at string formatting