What Is Type Casting In Python With Example?
In Python, Type casting is a method used to change the variables/ values declared in a particular data type into another data type to match the operation required to be performed by the code snippet. In python, this feature can be accomplished by using constructor functions like int(), string(), float(), char(), bool(), etc.
Type Casting Functions are:
- int()
- float()
- complex()
- bool()
- str()
int() type casting in python
Here we will try to convert other data types into int data types for example.
- float() to int() conversion in python
Here we will learn to convert a float value into an integer value.
Syntax:
int(float_value)
Example -1:
int(10.989) #output: 10
Example – 2:
a = 10.989 b = int(a) print('Type of a is: ', type(a)) print('Type of a is: ',type(b))
Output:
Type of a is: <class 'float'> Type of a is: <class 'int'>
- Complex number to int()/ complex → int()
In Python, we can’t convert complex numbers into integer numbers.
Example:
int( 10 + 20j ) # Error
Output:
Traceback (most recent call last): File "<string>", line 1, in <module> TypeError: can't convert complex to int
- Bool to int Python
Convert the boolean value into an integer value. Before seeing the example we must have to know that True → 1 and False → 0 in Python.
Syntax:
int( boolean_value ) int( boolean_value )
Example:
print(int( True )) #Output → 1 print(int( False )) #Output → 0
- Convert String to int in Python
Here we will see, how we can convert string to int type.
- String internally contains the only integer value that should be specified base 10
Example:
print(int('15')) #Output → 15 print(int(0B111)) # 0B111 is binary value, 0B represented as binary & 111 is binary value. #Output → 7 print(int('10.5')) # ValueError: invalid literal for int() with base 10: '10.5'
- float to int in python
While converting float to int type it will remove the decimal value and it will print output only integer value.
Example:
print(int(12.134)) #Output → 12 print(int(11023.234)) #Output → 11023
Float() Type Casting In Python
Now we will convert other data types to float types.
Syntax:
float(value)
- Int to Float in python
Here we will convert an integer value into a float value. Let’s check with an example.
Syntax:
float(int_value)
Example:
print(float(15)) #Binary value to float conversion print(float(0B111)) #Hexadecimail to float conversion print(float(0XFace))
Output:
15.0 7.0 64206.0
- Complex to Float in Python
- Complex to-float conversion is not possible.
Example:
float(10 + 20j ) # Output → TypeError: can't convert complex to float
- Boolean to float in Python
True and False consider as a boolean value, where True → 1 and False → 0
Example:
float( True ) #Output → 1.0 float( False ) #Output → 0.0
- convert string to float in python
To convert a string into a float, the String should contain an int value or float value with a base of 10.
Example:
float( ‘10’ ) #Output → 10.0 float ( ‘10.5’ ) #Output → 10.5 float('oXFace' ) #ValueError: could not convert string to float: 'oXFace' float('A') #ValueError: could not convert string to float: 'A' float('a') #ValueError: could not convert string to float: 'a'
Complex() Type Casting
Here we will learn to convert complex number types into other data types like int to complex, float to complex, a string to complex, etc.
Syntax:
- complex(x)
- complex(x, y)
Where x is a real number and y is an imaginary number
- int to complex() Python
Let’s here we will learn to convert an integer number to a complex number.
Example1:
In this example, we will convert an integer number into a complex number using only a real number.
complex(10) #Output→ (10 + 0j) complex(0B1111) #Output → (15 + 0j)
Example2:
In this example, we will convert an integer number into a complex number using both a real number & an imaginary number.
complex(10,20) #Output → (10 + 20j) complex(0B1111, 0B1010) #Output → (15 + 10j)
- float to complex() Python
Let’s here we will learn to convert a float number to a complex number.
Example1:
In this example, we will convert a float number into a complex number using only a real number.
complex(10.5) #Output→ (10.5 + 0j)
Example2:
In this example, we will convert a float number into a complex number using both a real number & an imaginary number.
complex( 10.5, 20.6 ) #Output → (10.5 + 20j.6)
- Boolean to complex() Python
Let’s here we will learn to convert a boolean number to a complex number.
Example1:
In this example, we will convert a boolean number into a complex number using only a real number.
complex(True) #Output→ (1 + 0j) complex(False) #Output → (0 + 0j)
Example2:
In this example, we will convert an integer number into a complex number using both a real number & an imaginary number.
complex(True, 20) #Output → (1 + 20j) complex(False, 10) #Output → (10j) complex(10, True) #Output → (10 + 1j) complex(10, False) #Output → (10 + 0j)
- String to Complex()
Let’s here we will learn to convert a string number to a complex number. Int and float should be based on 10.
Example1:
In this example, we will convert a string number into a complex number using only a real number.
complex(“10”) #Output→ (10 + 0j) complex(“10.5”) #Output → (10.5 + 0j)
Example2:
In this example, we will convert a string number into a complex number using both a real number & an imaginary number.
#Both should not be a string value
complex(“10”, “20”)
#SyntaxError: invalid character in an identifier
complex(“10”,20 )
#complex() can’t take the second arg if the first is a string
Bool() Type:
Here we will learn to convert bool number types into other data types like int to bool, float to bool, etc.
Syntax:
bool(value)
- int to bool
bool(10) #Output → True bool(0) #Output → False bool(-10) #Output → True
- float to bool
bool(0.1) #Output → True bool(0.0) #Output → False bool(1.0) #Output → True bool(-0.0001) #Output → True
- Complex to bool
If both real and imaginary parts are zero ‘0’ then only False else True.
Example:
bool( 0 + 0j ) #Output → False bool( 1 + 0j ) #Output → True
- Str to bool
If any string is available then the output is True and if a string is empty then the output is False.
Example
bool( ‘True’ ) #Output → True bool( ‘False’ ) #Output → True bool( ‘yes’ ) #Output → True bool( ‘no’ ) #Output → True bool( ‘ ’ ) #Output → False
String Type/ Str() Type In Python
Any value can be easily converted into a string type.
Syntax:
str(value)
- int to string Type
str(10) #Output → ‘10’ #binary value to string type str(0B1111) #Output → ‘15’
- Float to string type conversion
str(10.5) #Output → ‘10.5’ str(0.111) #Output → ‘0.111’
- Complex to string type conversion
str(10 + 20j) #Output → (10 + 20j) str(True + 0j) #Output → (1+0j) str(False + 0j) #Output → 0j str(False + 10j) #Output → 10j str(True + 10j) #Output → (1 + 10j)
- Bool-to-string type conversion
str(True) #Output → ‘True’ str(False) #Output → ‘False’
Type Casting Summary
Typecasting Evaluation\ Arg type | Int arg | Float arg | Complex arg | Bool arg | Str arg |
int() | yes | yes | no | yes | Contain only int value with base 10 |
float() | Yes | yes | no | yes | Contain int or float value with base 10 |
complex() | yes | yes | yes | yes | 1st arg. should any value but 2nd arg must be int. |
bool() | yes | yes | yes | yes | yes |
str() | yes | yes | yes | yes | yes |