Type casting in Python means converting a value from one data type to another. It is very useful when working with different kinds of data, such as combining strings and numbers or making sure numeric calculations happen correctly.
Python supports:
int(), float(), str(), and bool().int + float).input() is always a string and usually needs casting.int → float).Common explicit casting functions in Python:
int(x) – converts x to an integer (decimals are truncated, not rounded).float(x) – converts x to a floating-point number.str(x) – converts x to a string.bool(x) – converts x to a Boolean value (True or False).When Python performs an arithmetic operation between an int and a float, it will implicitly convert the int to float so that precision is not lost.
In implicit casting, Python automatically converts one type to another during an operation.
x = 10 # int
y = 3.5 # float
z = x + y # z becomes float (13.5)
print(z)
print(type(z))
You can manually convert values to the type you need using casting functions.
x = int(3.9) # x = 3
y = float("4.2") # y = 4.2
z = str(100) # z = "100"
print(x, y, z)
Most real programs take input from users as strings and then cast them before calculations.
age = input("Enter your age: ") # input returns string
age = int(age) # convert to integer
print("Next year, you will be", age + 1)
x + y becomes 13.5 and type(z) is <class 'float'>, showing that Python upgraded int to float.int(3.9) gives 3 (decimal part is removed).float("4.2") converts the string "4.2" into a float value.str(100) converts the number 100 into the string "100", which can be concatenated with other strings.input() returns a string. Using int(age) allows arithmetic like age + 1 to work correctly.int() truncates decimals (e.g., int(3.9) becomes 3, not 4).str() or use f-strings, e.g., f"Age: {age}"."abc" to int or float — they will raise a ValueError.int() and print the result.input(), convert it into an integer using int(), and add 10 to it.str() or an f-string.bool() on values like 0, 1, empty strings "", and non-empty strings to see which are True or False.