How To Convert Data Types in Python 3? - GeeksforGeeks

25 Aug, 2021
Prerequisites: Python Data Types
Type conversion is the process of converting one data type to another. There can be two types of type conversion in Python –
Implicit Type Conversion
Explicit Type Conversion
It is a type of type conversion in which handles automatically convert one data type to another without any user involvement.
Example:
Python3
# Python program to demonstrate# implicit type conversion # Python automatically converts# a to inta = 5print(type(a)) # Python automatically converts# b to floatb = 1.0print(type(b)) # Python automatically converts# c to int as it is a floor divisionc = a//bprint(c)print(type(c))
Output:
<class 'int'>
<class 'float'>
5.0
<class 'float'>
In the above example, it can be seen that Python handles all the type conversion automatically without any user involvement.
In Explicit type conversion, user involvement is required. The user converts one data type to another according to his own need. This can be done with the help of str(), int(), float(), etc. functions. Let’s see the handling of various type conversions.
A string is generally a sequence of one or more characters. We are often required to convert string to numbers and vice versa. Let’s see each of them in detail.
A number can be converted to string using the str() function. To do this pass a number or a variable containing the numeric value to this function.
Example:
Python3
# Python program to demonstrate# type conversion of number to# string a = 10 # Converting number to strings = str(a)print(s)print(type(s))
Output:
10
<class 'str'>
This can be useful when we want to print some string containing a number to the console. Consider the below example.
Example:
Python3
s = "GFG"n = 50 print("String: " + s + "\nNumber: " + str(n))
Output:
String: GFG
Number: 50
A string can be converted to a number using int() or float() method. To do this pass a valid string containing the numerical value to either of these functions (depending upon the need).
Note: If A string containing not containing a numeric value is passed then an error is raised.
Example:
Python3
# Python program to demonstrate# type conversion of string to# number s = '50' # Converting to intn = int(s)print(n)print(type(n)) # Converting to floatf = float(s)print(f)print(type(f))
Output:
50
<class 'int'>
50.0
<class 'float'>
There are basically two types of numbers in Python – integers and floating-point numbers. Weare often required to change from one type to another. Let’s see their conversion in detail.
A floating-point can be converted to an integer using the int() function. To do this pass a floating-point inside the int() method.
Example:
Python3
# Python program to demonstrate# floating point to integer f = 10.0 # Converting to integern = int(f)print(n)print(type(n))
Output:
10
<class 'int'>
An integer can be converted to float using the float() method. To do this pass an integer inside the float() method.
Example:
Python3
# Python program to demonstrate# integer to float n = 10 # Converting to floatf = float(n)print(f)print(type(f))
Output:
10.0
<class 'float'>
In Python, Both tuple and list can be converted to one another. It can be done by using the tuple() and list() method. See the below examples for better understanding.
Example:
Python3
# Python program to demonstrate# type conversion between list# and tuples t = (1, 2, 3, 4)l = [5, 6, 7, 8] # Converting to tupleT = tuple(l)print(T)print(type(T)) # Converting to listL = list(t)print(L)print(type(L))
Output:
(5, 6, 7, 8)
<class 'tuple'>
[1, 2, 3, 4]
<class 'list'>
ruhelaa48
sagar0719kumar
Python-Data Type
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
Reading and Writing to text files in Python
*args and **kwargs in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python