Friday, December 27, 2024
Google search engine
HomeLanguagesHow To Convert Data Types in Python 3?

How To Convert Data Types in Python 3?

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

Implicit 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 int
a = 5
print(type(a))
 
# Python automatically converts
# b to float
b = 1.0
print(type(b))
 
# Python automatically converts
# c to int as it is a floor division
c = a//b
print(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.

Explicit Type Conversion

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.

Type Conversion with strings

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.

Converting Numbers to String

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 string
s = 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

Converting String to Number

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 int
n = int(s)
print(n)
print(type(n))
 
# Converting to float
f = float(s)
print(f)
print(type(f))


Output:

50
<class 'int'>
50.0
<class 'float'>

Type Conversion with Numbers

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.

Floating Point to Integer

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 integer
n = int(f)
print(n)
print(type(n))


Output:

10
<class 'int'>

Integer to Floating Point

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 float
f = float(n)
print(f)
print(type(f))


Output:

10.0
<class 'float'>

Type conversion between Tuple and List

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 tuple
T = tuple(l)
print(T)
print(type(T))
 
# Converting to list
L = list(t)
print(L)
print(type(L))


Output:

(5, 6, 7, 8)
<class 'tuple'>
[1, 2, 3, 4]
<class 'list'>

RELATED ARTICLES

Most Popular

Recent Comments