Type casting in Python is used to convert one type of value into another possible type, allowing compatibility and flexibility between different data types.
Python provides five fundamental type-casting techniques.
int()
float()
bool()
complex()
str()
1. int()
The int() function in Python is used to convert a value of a different type into an integer type.
float values convert into an int.
# float to int
var = 100.75
print(var,type(var))
var = int(var)
print(var,type(var))
bool values convert into int.
# bool to int
var = True
print(var,type(var))
var = int(var)
print(var,type(var))
Complex values cannot be converted into int. It shows an error.
# complex to int
var = 2+3j
print(var,type(var))
var = int(var)
print(var,type(var)) # error as Type Error
We can convert it into string int into int only.
# str int to int
var = "23"
print(var,type(var))
var = int(var)
print(var,type(var))
The pure string we can convert into int
# value error
var = "Python"
print(var,type(var))
var= int(var)
print(var,type(var))
# string float we cannot convert into int it shows error - value error
var = "12.34"
print(var,type(var))
var = int(var)
print(var,type(var))
# string complex we cannot convert into int it shows error - values error
var = "2+3j"
print(var,type(var))
var = int(var)
print(var,type(var))
2. float()
The floatt() function in Python is used to convert a value of a different type into a float type.
we can convert the int data type to the float data type.
# int to float data type
var = 20
print(var,type(var))
var = float(var)
print(var,type(var))
we can convert the bool data type to the float data type.
bool to float data type
var = True
print(var,type(var))
var = float(var))
print(var,type(var))
We cannot convert complex data types to float data types. It shows the error as a value error
# complex to float data type
var = 3+4j
print(var,type(var))
var = float(var))
print(var,type(var))
We can only convert a string float data type into a float data type.
# string float into float
var = "23.34"
print(var,type(var))
var = float(var))
print(var,type(var))
3) bool()
The bool() is used to convert any Possible type values into bool type values.
All NON-Zero Values are Treated as True.
All zero values and empty strings are considered false.
We will convert all the data types into bool data type
# int to bool
var = 23
print(var,type(var))
var = bool(var)
print(var,type(var))
var1 = 0
print(var1,type(var1))
var1 = bool(var1)
print(var1,type(var1))
# flaot to bool
var = 22.5
print(var,type(var))
var = bool(var)
print(var,type(var))
# complex to bool
var = 3+4j
print(var,type(var))
var = bool(var)
print(var,type(var))
# str to bool
var = ""
print(var,type(var))
var = bool(var)
print(var,type(var))
var1 = "False"
print(var,type(var))
5) Str()
str() is used for converting "All Types of values into str type values"
We cannot convert a pure string into an int, float, or complex.
# Pure str into other data types
var = "hello"
print(var,type(var))
var1 = int(var) # error
var2 = float(var) # error
var3 = complex(var) # error
We can convert a str into a bool data type.
# srt to bool
var = "hello"
print(var,type(var))
var = bool(var)
print(var,type(var))