To make sure a string will display as expected, we can format the result with the format() method or using f-strings (available in Python 3.6+).
F-strings are the modern, recommended way to format strings in Python due to their readability and performance.
To specify a string as an f-string, simply put an f in front of the string literal, and add curly brackets {} as placeholders for variables and other operations.
name = "John"
age = 36
txt = f"My name is {name}, I am {age}"
print(txt)
F-strings can also evaluate Python expressions directly inside the curly brackets!
price = 59
tax = 0.25
txt = f"The total price is {price + (price * tax)} dollars"
print(txt)
format() MethodThe format() method is an older but still very common way to format strings. It takes the passed arguments, formats them, and places them in the string where the placeholders {} are.
quantity = 3
itemno = 567
price = 49
myorder = "I want {} pieces of item number {} for {:.2f} dollars."
print(myorder.format(quantity, itemno, price))