Knowledge organisersData Types
converting data types during the execution of a program e.g. Str() Int() Float()
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().
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.staffID = surname + str(year) — str() casts the integer year to a string for concatenation.int(message.right(4)) * 2 — casts the last 4 characters to integer before multiplying.# 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