04-casting.py
python-programming / programming-foundations  ·  Lesson 04
Lesson 04 — Programming foundations

Casting

Temporarily convert values from one data type to another.

Casting changes the type of a value. It is especially important because keyboard input arrives as text even when the user types digits.

Predict

Read the code first. Do not run it yet.

casting-predict.py
number = "12"
print(number + "3")
print(int(number) + 3)
Predict before running
  • Predict both outputs.
  • Why does the first line join text instead of adding numerically?
  • What does int() change?

Investigate

Use this code to explain exactly how the program works.

casting-investigate.py
age = int(input("Age: "))
next_age = age + 1
print(next_age)
Investigate the code
  • Which operation performs the cast?
  • What type does input() return before casting?
  • Why is the cast needed before addition?

Common casts

CastPurposeExample
int()convert to an integerint("14") → 14
float()convert to a real numberfloat("2.5") → 2.5
str()convert to textstr(25) → "25"
price.py
price = float(input("Price: "))
quantity = int(input("Quantity: "))
total = price * quantity
print("Total:", total)
The text must be suitableint("ten") cannot produce an integer. Validation is covered later.

Self-study guide

Casting temporarily converts a value from one data type to another. It is useful when the current type is unsuitable for the next operation—for example, converting keyboard input from text into a number before arithmetic.

Casting does not magically make every value convertible. int("15") works, but int("fifteen") does not because that text is not a valid integer representation.

OCR focusUnderstand that data types can be temporarily changed through casting and explain where that is useful. OCR ERL lists str(), int(), float(), real() and bool(); Python uses str(), int(), float() and bool() but has no real() function.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
int("3")int("3")Converts suitable text or a compatible value to an integer.
float("4.52")float("4.52")Converts to a Python floating-point value.
real("4.52")float("4.52")OCR ERL can use real(); Python normally uses float().
str(345)str(345)Converts a value to text.
bool(...)bool(...)Boolean conversion exists in both, but be careful: bool("False") is True in Python because a non-empty string is truthy.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

A very common pattern is input → cast → calculate → output.

lesson-04-worked.py
age_text = input("Age: ")
age = int(age_text)
next_age = age + 1

print("Next age:", next_age)
Output
Next age: 16
Work through it line by line
  • input() returns the text "15".
  • int(age_text) creates the integer 15.
  • Only after casting can the program safely perform integer arithmetic.
  • The original variable age_text remains a string; the converted result was stored separately in age.

Another example

Casting can also be useful when joining a number into text.

lesson-04-example-2.py
score = 42
message = "Score: " + str(score)
print(message)
Output
Score: 42
Why this example matters
  • String concatenation requires strings on both sides of +.
  • str(score) provides a text version of the numeric value.

Common mistakes

Avoid these errors
  • Assuming input() returns an integer automatically.
  • Using int() for decimal text such as "4.5"; use float() instead.
  • Using bool("False") expecting False; non-empty strings are truthy in Python.
Exam-safe warningChoose the cast that matches the data you actually need. Casting is purposeful conversion, not something to apply to every value.

Exam-ready checklist

You should now be able to
  • Explain why casting is needed in a given program.
  • Use int(), float() and str() appropriately in Python.
  • Recognise OCR real() as corresponding to Python float() in this course.
  • Predict whether a cast is valid for a particular value.

What you have learned

  • Casting temporarily changes a value from one type to another.
  • Use int(), float() and str() when appropriate.
  • Numeric keyboard input normally needs casting before arithmetic.