Menu Close

Type Conversion in Python

In this article, we are going to learn all about Type conversion in Python and its type with the help of examples. Type conversion in Python is a way to convert the value of one data type ( Data type may be an integer, float, string, etc) to another data type. The process of this conversion in Python is called type conversion.

What is Type Conversion in Python?

Python type conversion is also called type casting, so you should not have any confusion between type conversion and type casting.

Type conversion in Python is the process of converting one data type to another data type. In python
various types of type conversions are available. In this article, you will learn various types of type conversion.

In python programming, every value has a data type. The data type is the classification of data that tells the compiler what type of data the programmer wants to use.

Python provides the following function which is used for type conversion or type casting.

Functions to perform Python type conversion


FunctionDescription
int()This function converts any data type to an integer.
float()This function is used to convert any data type to a floating-point number.
str()This function is used to convert integer to string.
list()This function is used to convert any data type to a list type.
tuple()This function is used to convert any data type to a tuple data type.
dict()This function is used to a tuple of order (key, value) into the dictionary.
set()The set() function is used to convert any data type to set data type.
complex()The complex(real, image) function is used to convert real numbers to complex numbers.
ord()The ord() function is used to convert character to string.
hex()hex() function is used to convert an integer to a hexadecimal string.
oct()oct() function is used to convert an integer to a decimal string.

You have to remember some points regarding Python type conversion.

  • Type conversion is the process of the conversion of an object from one data type to another data type.
  • Implicit type conversion is automatically performed by a Python interpreter.
  • Python avoids loss of data during implicit type conversion.
  • Explicit type conversion is called type conversion or typecasting, where data of objects are converted into another data type by using a predefined Python function.

Python has a two type of type conversion.

  • Implicit type conversion
  • Explicit type conversion

Implicit Type Conversion in Python.

In Implicit type conversion. Python automatically convert one data type to another data type.

Example: Implicit Type Conversion in Python

a = 12
b = 12.0

result = a + b

print('Value of a is:- ',a)
print('type of a is:- ',type(a))

print('Value of b is:- ', b)
print('type of b is:- ',type(b))

print('Value of result is:- ', result)
print('type of result is:- ', type(result))

Output

 Value of a is:-  12
 type of a is:-  <class 'int'>
 Value of b is:-  12.0
 type of b is:-  <class 'float'>
 Value of result is:-  24.0
 type of result is:-  <class 'float'>

Note:- use type() function to check the data type of python object.

Example 2: Add string to Integer

Adding string data type to integer data type.

first = 121
second = "Python"

print('Value of first is:- ',first)
print('type of first is:- ',type(first))

print('Value of second  is:- ',second)
print('type of second is:- ',type(second))

# add integer to string
result = first + second

print('Value of result is:- ',result)
print('type of result is:- ',type(result))

When you run the above example than output will be.

 Value of first is:-  121
 type of first is:-  <class 'int'>
 Value of first is:-  Python
 type of second is:-  <class 'str'>
 Traceback (most recent call last):
   File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 12, in <module>
     result = first + second
 TypeError: unsupported operand type(s) for +: 'int' and 'str'


To solve this problem Python provides explicit type conversion which can be discussed below.

Explicit Type Conversion in Python

In Explicit type conversion, Users convert one data type to another data type. In python there are various functions are available are used for explicit type conversion or type casting.

Example:

first = 121
second = "Python"

print('Value of first is:- ',first)
print('type of first is:- ',type(first))

print('Value of second is:- ',second)
print('type of second is:- ',type(second))

# add integer to string
result = str(first) + second

print('Value of result is:- ',result)
print('type of result is:- ',type(result))

Output

Value of first is:-  121
type of first is:-  <class 'int'>
Value of second is:-  Python
type of second is:-  <class 'str'>
Value of result is:-  121Python
type of result is:-  <class 'str'>

Integer to Float type conversion

To convert Integer data type to Float type use Python float() function.

Example:- Convert integer to float in Python

a = 30
print('value of a is:- ',a)
print('type of a is:- ',type(a))

# convert int to float
b = float(a)
print('value of a is:- ',b)
print('type of a is:- ',type(b))

# Output
# value of a is:-  30
# type of a is:-  <class 'int'>
# value of a is:-  30.0
# type of a is:-  <class 'float'>

Float to Integer type conversion

To convert Float data type to Integer type use Python int() function.

Example: Convert float to integer in Python

a = 30.0
print('value of a is:- ',a)
print('type of a is:- ',type(a))

# convert float to integer
b = int(a)
print('value of a is:- ',b)
print('type of a is:- ',type(b))


# Output

# value of a is:-  30.0
# type of a is:-  <class 'float'>
# value of a is:-  30
# type of a is:-  <class 'int'>

Integer to String type conversion

To convert Integer data type to String type use Python str() function.

Example: Convert integer to string in Python

a = 30
print('value of a is ',a)
print('type of a is ',type(a))

# convert integer to string
b = str(a)
print('value of a is ',b)
print('type of a is ',type(b))

# Output
# value of a is  30
# type of a is  <class 'int'>
# value of a is  30
# type of a is  <class 'str'>

String to Integer type conversion

To convert String data type to Integer type use int() function.

Example: String to Integer conversion

a = '5121'
print('value of a is ',a)
print('type of a is ',type(a))

# convert string to integer
b = int(a)
print('value of a is ',b)
print('type of a is ',type(b))

# Output
# value of a is  5121
# type of a is  <class 'str'>
# value of a is  5121
# type of a is  <class 'int'>

Integer to Complex type conversion

To convert Integer data type to Complex type use Python complex() function.

Example: Convert Integer to Complex in Python

a = 12
print('value of a is:- ',a)
print('type of a is:- ',type(a))

# integer to complex
b = complex(a)
print('value of a is:- ',b)
print('type of a is:- ',type(b))

# Output
# value of a is:-  12
# type of a is:-  <class 'int'>
# value of a is:-  (12+0j)
# type of a is:-  <class 'complex'>

Float to Complex type conversion

To convert Integer data type to Complex type use complex() function.

Example: Convert Float to Complex in Python

a = 12.0
print('value of a is ',a)
print('type of a is ',type(a))

# float to complex 
b = complex(a)
print('value of a is ',b)
print('type of a is ',type(b))

# Output
# value of a is  12.0
# type of a is  <class 'float'>
# value of a is  (12+0j)
# type of a is  <class 'complex'>

String to list conversion

To convert String data type to List data type use list() function.

Example: String to list in Python

a = 'Programming Funda'
print('value of a is ',a)
print('type of a is ',type(a))

# string to list
b = list(a)
print('value of a is ',b)
print('type of a is ',type(b))

String to Set Type Conversion

To convert String data type to Set data type use Python set() function.

Example: Convert string to set in Python

a = 'Programming Funda'
print('value of a is ',a)
print('type of a is ',type(a))

# convert string to set
b = set(a)
print('value of a is ',b)
print('type of a is ',type(b))

# Output

# value of a is  Programming Funda
# type of a is  <class 'str'>
# value of a is  {'P', 'n', 'u', 'r', 'd', 'i', 'a', 'o', 'g', 'm', 'F', ' '}
# type of a is  <class 'set'>

String to Tuple Conversion

To convert String data type to Tuple data type use Python tuple() function.

Example: Convert string to Tuple Conversion

a = 'Programming Funda'
print('value of a is ',a)
print('type of a is ',type(a))

# convert string to list
b = tuple(a)
print('value of a is ',b)
print('type of a is ',type(b))

Tuple to Dict Type Conversion

To convert Tuple data type to Tuple data type use tuple() function.

Example: Convert Tuple to Dict in Python

a = (('Python',1),('JavaScript',2),('Java',3))
print('value of a is ',a)
print('type of a is ',type(a))

# convert Dict to tuple
b = dict(a)
print('value of a is ',b)
print('type of a is ',type(b))

# Output
# value of a is  (('Python', 1), ('JavaScript', 2), ('Java', 3))
# type of a is  <class 'tuple'>
# value of a is  {'Python': 1, 'JavaScript': 2, 'Java': 3}
# type of a is  <class 'dict'>

Character to Integer

To convert Character data type to Integer data type use Python ord() function.

Example: Convert Character to Integer in Python

a = '5'
print('value of a is ',a)
print('type of a is ',type(a))

b = ord(a)
print('value of a is ',b)
print('type of a is ',type(b))

# Output
# value of a is  5
# type of a is  <class 'str'>
# value of a is  53
# type of a is  <class 'int'>

Integer to Hexadecimal

To convert Integer data type to Hexadecimal data type use Python hex() function.

Example: Convert Integer to hexadecimal

a = 58
print('value of a is: ',a)
print('type of a is: ',type(a))

b = hex(a)
print('value of a is: ',b)
print('type of a is: ',type(b))

# Output
# value of a is:  58
# type of a is:  <class 'int'>
# value of a is:  0x3a
# type of a is:  <class 'str'># 

Integer to Octal String

To convert Integer data type to Octal data type use Python oct() function.

Example:- Convert Integer to octal in Python

a = 58
print('value of a is ',a)
print('type of a is ',type(a))

b = oct(a)
print('value of a is:- ',b)
print('type of a is:- ',type(b))

# Output
# value of a is  58
# type of a is  <class 'int'>
# value of a is:-  0o72
# type of a is:-  <class 'str'>

Convert List to Tuple, Set, and String

You can convert tuple, set and string from Python list as well.

Example:

myList = [1,2,3,4,5,6,7]

# convert list to tuple
print("The new tuple is:- ", tuple(myList))

#convert list to set
print("The new set is:- ", set(myList))

# convert list to string
lst = ['P','y','t','h','o','n']
print("The new string is:- ", ''.join(lst))

# Output
# The new tuple is:-  (1, 2, 3, 4, 5, 6, 7)
# The new set is:-  {1, 2, 3, 4, 5, 6, 7}
# The new string is:-  Python

Conclusion

In this article, we have demonstrated all about Python type conversion with the help of The examples. There is Python predefined function to convert objects from one data type to another data type.

Type conversion in Python is going to be very helpful when you’re working with Python real-life project because sometimes you need to convert data from one data type to another data type, coming from the client-side, If you are working with APIs.

I hope this article will helpful for you, Please share and keep visiting for further Python tutorials.

~Thanks for reading

Python Ternary Operator
Iterators in Python ( With Examples )

Related Posts