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

Knowledge organisers / Data Types and Casting

Casting

All topicsPractise exam questions
Knowledge organiser

Data Types and Casting

301.4c

Convert between strings & numbers.

What you need to know

Casting converts a value from one type to another. Use int(), float(), and str() to convert. This is essential when working with input(), which always returns a string.

Key points

  • int("5") converts string to integer → 5
  • float("3.14") converts string to float → 3.14
  • str(42) converts integer to string → "42"
  • int(3.9) truncates to 3 (does not round)

Code examples

Casting examples
python
num_str = "42"
num = int(num_str)     # 42 as int
pi = float("3.14")    # 3.14 as float
label = str(100)       # "100" as string