What is type casting in Python with example? | Type Conversion

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

Int Type Casting In Python

Here we will try to convert other data types into int data types for example.

  1. 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'>
  1. 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
  1. 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
  1. 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'
  1. 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

Float() Type Casting In Python

Now we will convert other data types to float types.

Syntax:

float(value)
  1. 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
  1. Complex to Float in Python
  • Complex to-float conversion is not possible.

Example:

float(10 + 20j )ย 
# Output โ†’ TypeError: can't convert complex to float
  1. 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
  1. 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

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

  1. 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)
  1. 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)
  1. 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:

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)
  1. int to bool
bool(10) #Output โ†’ True
bool(0) #Output โ†’ False
bool(-10) #Output โ†’ True
  1. float to bool
bool(0.1) #Output โ†’ True
bool(0.0) #Output โ†’ False
bool(1.0) #Output โ†’ True
bool(-0.0001) #Output โ†’ True
  1. 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
  1. 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

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 typeInt argFloat argComplex argBool argStr arg
int()yesyesnoyesContain only int value with base 10
float()Yesyesย noyesContain int or float value with base 10
complex()yesyesyesyes1st arg. should any value but 2nd arg must be int.
bool()yesyesyesyesyes
str()yesyesyesyesyes

Python Quizzes:

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.