In Ruby I can do this:
"This is a string with the value of #{variable} shown."
How do I do that same thing in Python?
In Ruby I can do this:
"This is a string with the value of #{variable} shown."
How do I do that same thing in Python?
The modern/preferred way is to use str.format
:
"This is a string with the value of {} shown.".format(variable)
Below is a demonstration:
>>> 'abc{}'.format(123)
'abc123'
>>>
Note that in Python versions before 2.7, you need to explicitly number the format fields:
"This is a string with the value of {0} shown.".format(variable)
You have a lot of options.
"This is a string with the value of " + str(variable) + " shown."
"This is a string with the value of %s shown." % (str(variable))
"This is a string with the value of {0} shown.".format(variable)
this is one of the way we can also do
from string import Template
s = Template('$who likes $what')
s.substitute(who='tim', what='kung pao')