Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Data Types

Casting

All topicsPractise exam questions
Knowledge organiser

Data Types

2.2.2b

converting data types during the execution of a program e.g. Str() Int() Float()

What you need to know

Casting is the process of temporarily converting a value from one data type to another. This is often needed when input is received as a string but needs to be used as a number, or when a number needs to be joined to a string for output. Common casting functions include int(), float(), str(), and bool().

Key points

  • Definition:Casting: converting/changing one data type to another.
  • int() converts a value to an integer, e.g. int("5") gives 5.
  • float() or real() converts a value to a decimal number, e.g. float("3.14") gives 3.14.
  • str() converts a value to a string, e.g. str(42) gives "42". Needed for concatenation with strings.
  • bool() converts a value to a Boolean, e.g. bool(1) gives True.
  • Input from the user is always received as a string, so casting to int or float is often needed before calculations.
  • Exam Tip:The definition is 'converting one data type to another' — not 'change to string' (that's an example, not a definition).
  • Exam Example:staffID = surname + str(year) — str() casts the integer year to a string for concatenation.
  • Exam Example:int(message.right(4)) * 2 — casts the last 4 characters to integer before multiplying.

Code examples

Casting in Python
python
# Input is always a string, so we cast to int
age = int(input("Enter your age: "))

# Cast int to string for concatenation
print("You are " + str(age) + " years old")

# Cast string to float
price = float("9.99")
print(price + 1)  # Output: 10.99