Variables, numbers, strings, and f-strings

Store and reuse values, do arithmetic, and build clean text with f-strings instead of clunky concatenation.

🌱 Module 0 7 min read Not started

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:

  1. Create a variable and read its value back.
  2. Name variables the way Python developers expect.
  3. Do simple arithmetic with numbers.
  4. Work with text (strings) and combine them cleanly with f-strings.
  5. 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 =:

python
name = "Ada"
age = 30

Now name holds "Ada" and age holds 30. You can use them anywhere you would have typed the value:

python
print(name)     # Ada
print(age)      # 30

In 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:

python
print(name, age)

Prints Ada 30.

Reassigning

You can point a name at a new value at any time:

python
score = 0
score = score + 10     # take the old score, add 10, store it back
print(score)           # 10

The 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: price2 is fine, 2price is 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_name is better than n or x.

3. Numbers

Python has integers and floats.

python
count = 5          # int β€” a whole number
pi = 3.14159       # float β€” a number with a decimal

Basic arithmetic:

python
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  power

The two that confuse beginners are / and //:

  • 10 / 3 gives 3.3333... (a float).
  • 10 // 3 gives 3 (the whole-number part).
  • 10 % 3 gives 1 (the remainder, because 3 goes into 10 three times with 1 left over).

You can do arithmetic with variables too:

python
price = 20
quantity = 3
total = price * quantity
print(total)        # 60

4. Strings β€” text

A string is text inside quotes. Strings can use single or double quotes β€” both work; pick one and be consistent.

python
greeting = "hello"
name = 'Ada'

Combine strings with +

python
first = "Ada"
last = "Lovelace"
full = first + " " + last
print(full)     # Ada Lovelace

+ glues strings together. Note you must add the space yourself: " ".

Common string actions

python
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:

python
age = 30
message = "I am " + str(age) + " years old"   # clunky

The modern, clean way is an f-string. Put an f before the opening quote and braces around the value:

python
age = 30
name = "Ada"
print(f"I am {age} years old")          # I am 30 years old
print(f"{name} is {age}")               # Ada is 30

You can even put calculations inside the braces:

python
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.

python
# '5' is a string; 2 is an int. These two cannot be added.
"5" + 2        # ERROR: can only concatenate str, not int

You convert explicitly:

python
int("5")       # 5     string -> int
float("3.5")   # 3.5   string -> float
str(42)        # "42"  number  -> string

If the text is not a valid number, Python raises an error:

python
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

  1. Forgetting quotes around text. name = Ada β†’ NameError. Use name = "Ada".
  2. Mixing types. "5" + 2 β†’ TypeError. Convert first.
  3. Reusing = like math. x = x + 1 is not "x equals x plus 1"; it is "update x." It is fine and extremely common.
  4. Spelling a variable name differently. user_name and username are different names. Be consistent.
  5. Off-by-one with indexing. word[0] is the first character, not word[1]. Python counts from 0.

Hands-on lab (45 minutes)

  1. Create a variable name with your name and print a greeting using an f-string.
  2. Create item_price = 120 and tax = 0.18. Print the total (item_price * tax).
  3. Compute 17 // 5 and 17 % 5. Explain in one sentence what each gives.
  4. Print "Hello" + " " + "world" then rewrite it as an f-string.
  5. Convert "2026" to an int, add 4, and print "2030".
  6. Challenge: a rectangle is width = 8, height = 5. Print its area and its perimeter as "The perimeter is X".

Self-check

  1. What does = do in Python?
  2. What is the difference between / and //?
  3. Why use f-strings instead of +?
  4. What does int("5") + 2 give? What does "5" + "2" give?
  5. How do you get the last character of a string?

References

Sign in to save your progress and earn badges.