Putting a variable into a string (quote)
You should use a string formatter here, or concatenation. For concatenation you'll have to convert an int
to a string
. You can't concatenate ints and strings together.
This will raise the following error should you try:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Formatting:
quote = "You are %d years old" % age
quote = "You are {} years old".format(age)
Concatenation (one way)
quote = "You are " + str(age) + " years old"
Edit: As noted by J.F. Sebastian in the comment(s) we can also do the following
In Python 3.6:
f"You are {age} years old"
Earlier versions of Python:
"You are {age} years old".format(**vars())
This is one way to do it:
>>> age = 17
>>> quote = "You are %d years old!" % age
>>> quote
'You are 17 years old!'
>>>