Intro to Computer Science

Python Cheat Sheet

Print

Show something on the screen.

Show 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!

Show what is in a variable

Put an f before the opening quote, then wrap the variable in curly braces.

f-string with text

Use this one

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