String interpolation is a process of injecting value into a placeholder (a placeholder is nothing but a variable to which you can assign data/value later) in a string literal. It helps in dynamically formatting the output in a fancier way. Python supports multiple ways to format string literals.
With the release of Python 3.6, we were introduced to F-strings. As their name suggests, F-strings begin with "f" followed by a string literal. We can insert values dynamically into our strings with an identifier wrapped in curly braces, just like we did in the format() method.
Use parentheses In Python, you can freely break the line in parentheses ( () , {} , [] ). Using this rule, you can write a long string on multiple lines with parentheses instead of backslashes. Since {} is used for set and [] is used for list , use () for such purpose. Note that tuple is created by commas, not () .
What's a nice idiom to do this:
Instead of: print "%s is a %s %s that %s" % (name, adjective, noun, verb)
I want to be able to do something to the effect of: print "{name} is a {adjective} {noun} that {verb}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With