BeginnerPython · Lesson 2 of 8

Variables, Types & Strings

Numbers, text, booleans, None, f-strings and converting user input.

A variable is a name pointing to a value: score = 78. Python works out the type from the value — int (whole numbers), float (decimals), str (text), bool (True/False) and None (no value). Use type(x) to check.

Strings have many useful methods: .upper(), .strip(), .replace(), .split(). f-strings (f"...") insert values into text and can format them, e.g. {average:.1f} for one decimal place.

input() always returns a string. Convert it with int() or float() before doing arithmetic.

basics.pyPython
school = "Azania Secondary"   # str
students = 420                # int
fee = 150_000.50              # float (underscores make big numbers readable)
is_open = True                # bool
nickname = None               # no value yet

students += 15
print(type(students), students)

name = "  amina juma  "
clean = name.strip().title()
print(clean)                       # Amina Juma
print(clean.split())               # ['Amina', 'Juma']
print(len(clean), clean.lower())

scores = "78,64,91"
total = sum(int(s) for s in scores.split(","))
average = total / 3
print(f"{school}: total={total}, average={average:.1f}")

age_text = "17"            # imagine this came from input()
age = int(age_text)
print(f"Next year you will be {age + 1}")
Runs in your browser · Python

Key points

  • Python infers types; type() shows them.
  • f-strings are the clearest way to build text: f"{value:.2f}".
  • input() returns str — convert before calculating.

Exercise

Write a program that asks for a student's name and three scores using input(), then prints the name in title case and the average to one decimal place.

Show solution

Try the exercise yourself first — then compare your approach with this one.

input() always returns text, so convert each score with float() before adding. .strip().title() tidies the name, and :.1f in the f-string rounds the average to one decimal place.

average.pyPython
name = input("Student name: ")
score1 = float(input("Score 1: "))
score2 = float(input("Score 2: "))
score3 = float(input("Score 3: "))

average = (score1 + score2 + score3) / 3
print(f"{name.strip().title()}: average {average:.1f}")
Runs in your browser · Python
Example runText
Student name:   amina hassan
Score 1: 78
Score 2: 64
Score 3: 91
Amina Hassan: average 77.7

Check your understanding

  1. What type does input() return?

  2. What does f"{average:.1f}" print when average is 77.666?

  3. What is type(None)?

  4. What does " amina juma ".strip().title() return?

Ask AI