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.
number = "12"
print(number + "3")
print(int(number) + 3)- 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.
age = int(input("Age: "))
next_age = age + 1
print(next_age)- Which operation performs the cast?
- What type does
input()return before casting? - Why is the cast needed before addition?
Common casts
| Cast | Purpose | Example |
|---|---|---|
int() | convert to an integer | int("14") → 14 |
float() | convert to a real number | float("2.5") → 2.5 |
str() | convert to text | str(25) → "25" |
price = float(input("Price: "))
quantity = int(input("Quantity: "))
total = price * quantity
print("Total:", total)int("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.
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 / term | Python | What 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. |
Worked example
A very common pattern is input → cast → calculate → output.
age_text = input("Age: ")
age = int(age_text)
next_age = age + 1
print("Next age:", next_age)Next age: 16
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_textremains a string; the converted result was stored separately inage.
Another example
Casting can also be useful when joining a number into text.
score = 42
message = "Score: " + str(score)
print(message)Score: 42
- String concatenation requires strings on both sides of
+. str(score)provides a text version of the numeric value.
Common mistakes
- Assuming
input()returns an integer automatically. - Using
int()for decimal text such as"4.5"; usefloat()instead. - Using
bool("False")expectingFalse; non-empty strings are truthy in Python.
Exam-ready checklist
- Explain why casting is needed in a given program.
- Use
int(),float()andstr()appropriately in Python. - Recognise OCR
real()as corresponding to Pythonfloat()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()andstr()when appropriate. - Numeric keyboard input normally needs casting before arithmetic.