Casting in Python [Explained]
Casting in Python is done using functions like this:
- int() – convert Data type to integer
- float() – convert Data type to float
- str() – convert Data type to String
Int():
a = int(2.4)
b = int("8")
print (a)
print (type(a))
print (b)
print (type(b))
Output:
2
<class 'int'>
8
<class 'int'>
Float():
a = float(2)
b = int("8")
print (a)
print (type(a))
print (b)
print (type(b))
Output:
2.0
<class 'float'>
8
<class 'int'>
str():
a = str(2.4)
b = str(8)
print (a)
print (type(a))
print (b)
print (type(b))
Output:
2.4
<class 'str'>
8
<class 'str'>