Show something on the screen.
Print a message
Plain text
Start every print with f", even when there is no variable in the message yet.
print(f"Hello, world!")Output
Hello, world!
Start a new line with \n
\n inside the quotes starts a new line. That is a backslash, the key above Enter.
print(f"Guess a number.\nIt is between 1 and 10.")Output
Guess a number. It is between 1 and 10.
Print a variable
Put an f before the opening quote, then wrap the variable in curly braces.
f-string with text
The easiest way. Use this one.
name = "Becca"
print(f"My name is {name}. I like Minecraft.")Output
My name is Becca. I like Minecraft.
f-string with a number
Numbers need no extra work inside an f-string.
age = 17
print(f"I am {age} years old.")Output
I am 17 years old.
Math inside an f-string
You can do the math right inside the braces.
price = 3
qty = 4
print(f"Total: {price * qty}")Output
Total: 12
Forgetting the f
Without the f before the quote, Python prints the curly braces just as they are.
name = "Becca"
print("My name is {name}.")What happens
My name is {name}.