I am looking for a clean way to use variables within a multiline Python string. Say I wanted to do the following:
string1 = go
string2 = now
string3 = great
"""
I will $string1 there
I will go $string2
$string3
"""
I’m looking to see if there is something similar to $ in Perl to indicate a variable in the Python syntax.
If not – what is the cleanest way to create a multiline string with variables?
The common way is the
format()function:It works fine with a multi-line format string:
You can also pass a dictionary with variables:
The closest thing to what you asked (in terms of syntax) are template strings. For example:
I should add though that the
format()function is more common because it’s readily available and it does not require an import line.