String interpolation
When writing strings, you may want to include variables in the output. String interpolation includes the variable names as placeholders within the string. There are two standard methods for achieving string interpolation: comma separators and format.
Comma separators
Variables may be interpolated into strings using commas to separate clauses. It’s similar to the +
operator, except it adds spacing for you.
Look at the following example, where we add Ciao
within a print
statement:
italian_greeting = 'Ciao'
print('Should we greet people with', italian_greeting,
'in North Beach?')
The output is as follows:
Should we greet people with Ciao in North Beach?
f-strings
Perhaps the most effective way to combine variables with strings is with f-strings. Introduced in Python 3.6, f-strings are activated whenever the f
character is followed by quotations. The advantage is that any variable inside curly...