Python Fundamentals · Python Basics
Put variables and expressions straight into text, and control decimals and alignment with format specs.
item = "tea"
price = 2.5
qty = 3
cost = price * qty
receipt = f"{qty} x {item} = {cost}"
money = f"Total: {cost:.2f}" # .2f: always two decimal places
padded = f"[{item:>6}]" # >6: right-align in 6 characters
math = f"{qty + 1} cups next time" # any expression works inside { }
print(receipt)
print(money, padded, math)item is created with the value 'tea'.price is created with the value 2.5.qty is created with the value 3.:.2f formats the number with exactly two decimals: 7.50.:>6 right-aligns "tea" in 6 characters, padding with spaces on the left.3 x tea = 7.5.