Variables, numbers, strings, and f-strings
Store and reuse values, do arithmetic, and build clean text with f-strings instead of clunky concatenation.
Why this matters
Programs are useful because they remember things: a user's name, a price, a score, a piece of text from a website. Variables are how you store and reuse values by giving them names. Once you can store a value and then use it later, you can write programs that do more than print a fixed line β you can write programs that work on different input each time they run. That is the moment Python stops being a calculator and starts being a language.
Learning objectives
After this lesson you will be able to:
- Create a variable and read its value back.
- Name variables the way Python developers expect.
- Do simple arithmetic with numbers.
- Work with text (strings) and combine them cleanly with
f-strings. - Convert between numbers and text without crashing.
1. Variables β giving a value a name
A variable is a name that points to a value. You create one with =:
name = "Ada"
age = 30Now name holds "Ada" and age holds 30. You can use them anywhere you would have typed the value:
print(name) # Ada
print(age) # 30In Python, = does not mean "equals" in the math sense. It means "assign this value to this name." So age = 30 reads as "let the name age refer to the value 30."
Reading the value back
Use the name wherever you want its value:
print(name, age)Prints Ada 30.
Reassigning
You can point a name at a new value at any time:
score = 0
score = score + 10 # take the old score, add 10, store it back
print(score) # 10The line score = score + 10 works right-to-left: Python first works out the right side (0 + 10), then points the name score at that result.
2. Naming rules and style
Python is relaxed but has conventions. Follow them and your code reads well to everyone.
- Use lowercase letters, numbers, and underscores:
user_name,total_price. - Start with a letter or underscore, not a digit:
price2is fine,2priceis not. - Do not use Python's reserved words (like
if,for,while,class) as a variable name. - Use
snake_case(words separated by underscores) for variables and functions. - Use meaningful names.
user_nameis better thannorx.
3. Numbers
Python has integers and floats.
count = 5 # int β a whole number
pi = 3.14159 # float β a number with a decimalBasic arithmetic:
a = 10
b = 3
a + b # 13 addition
a - b # 7 subtraction
a * b # 30 multiplication
a / b # 3.333... true division (always a float)
a // b # 3 floor division (drops the remainder)
a % b # 1 remainder (modulo)
a ** b # 1000 powerThe two that confuse beginners are / and //:
10 / 3gives3.3333...(a float).10 // 3gives3(the whole-number part).10 % 3gives1(the remainder, because3goes into10three times with1left over).
You can do arithmetic with variables too:
price = 20
quantity = 3
total = price * quantity
print(total) # 604. Strings β text
A string is text inside quotes. Strings can use single or double quotes β both work; pick one and be consistent.
greeting = "hello"
name = 'Ada'Combine strings with +
first = "Ada"
last = "Lovelace"
full = first + " " + last
print(full) # Ada Lovelace+ glues strings together. Note you must add the space yourself: " ".
Common string actions
word = "python"
word.upper() # 'PYTHON'
word.lower() # 'python'
word.capitalize() # 'Python'
len(word) # 6 (length β how many characters)
word[0] # 'p' (first character; counting starts at 0)
word[-1] # 'n' (last character)
word[0:3] # 'pyt' (slice: characters 0,1,2)Do not worry about slicing yet β just know it exists. We cover it properly later.
5. f-strings β the clean way to build text
Building strings with + gets messy fast, especially with numbers:
age = 30
message = "I am " + str(age) + " years old" # clunkyThe modern, clean way is an f-string. Put an f before the opening quote and braces around the value:
age = 30
name = "Ada"
print(f"I am {age} years old") # I am 30 years old
print(f"{name} is {age}") # Ada is 30You can even put calculations inside the braces:
price = 20
quantity = 3
print(f"Total: {price * quantity}")f-strings are the way to build text in Python. Always prefer them over + string concatenation.
6. Converting between text and numbers
Sometimes a number is stored as a string (for example, text a user typed in a box), and you need to do math with it. Python will not let you mix them.
# '5' is a string; 2 is an int. These two cannot be added.
"5" + 2 # ERROR: can only concatenate str, not intYou convert explicitly:
int("5") # 5 string -> int
float("3.5") # 3.5 string -> float
str(42) # "42" number -> stringIf the text is not a valid number, Python raises an error:
int("hello") # ERROR: invalid literal for int()That error is fine β it tells you the input was not a number, and later you will learn how to handle that gracefully.
Common mistakes
- Forgetting quotes around text.
name = AdaβNameError. Usename = "Ada". - Mixing types.
"5" + 2βTypeError. Convert first. - Reusing
=like math.x = x + 1is not "x equals x plus 1"; it is "update x." It is fine and extremely common. - Spelling a variable name differently.
user_nameandusernameare different names. Be consistent. - Off-by-one with indexing.
word[0]is the first character, notword[1]. Python counts from 0.
Hands-on lab (45 minutes)
- Create a variable
namewith your name and print a greeting using an f-string. - Create
item_price = 120andtax = 0.18. Print the total (item_price * tax). - Compute
17 // 5and17 % 5. Explain in one sentence what each gives. - Print
"Hello" + " " + "world"then rewrite it as an f-string. - Convert
"2026"to an int, add4, and print"2030". - Challenge: a rectangle is
width = 8,height = 5. Print its area and its perimeter as "The perimeter is X".
Self-check
- What does
=do in Python? - What is the difference between
/and//? - Why use f-strings instead of
+? - What does
int("5") + 2give? What does"5" + "2"give? - How do you get the last character of a string?
References
Sign in to save your progress and earn badges.