Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are different data conversion methods in Python?
Data conversion in Python refers to changing the data type of a value to another data type. Python supports two different types of data conversion: Implicit conversion and Explicit conversion.
- Implicit Conversion is performed automatically by the Python interpreter.
- Explicit Conversion is manually done by the programmer using built-in functions.
Implicit Conversion
Python automatically converts one data type to another to avoid data loss during operations. The smaller data type is converted to a higher data type ?
Example
The following example shows how Python automatically converts an integer to a float during arithmetic operations ?
a = 13
print("The Data Type of Variable 'a' is:", type(a))
b = 6.9
print("The Data Type of Variable 'b' is:", type(b))
a = a - b
print("\nNow the value of 'a' is:", a)
print("Updated Data Type of the Variable 'a' is:", type(a))
The Data Type of Variable 'a' is: <class 'int'> The Data Type of Variable 'b' is: <class 'float'> Now the value of 'a' is: 6.1 Updated Data Type of the Variable 'a' is: <class 'float'>
As we can see, the variable a is initially an int, and variable b is a float. Python automatically changes the datatype of a to float to avoid data loss.
Explicit Conversion
Explicit conversion is also known as type casting, where the programmer uses predefined functions to convert one data type to another. Data loss may occur when forcing a value to a smaller data type.
Syntax
(required_data_type)(expression)
Example
The following example demonstrates explicit type conversion to fix a TypeError ?
a = 47
b = "51"
# This would cause TypeError: uncomment to see
# result1 = a + b
b = int(b) # Convert string to integer
result2 = a + b
print("Result after conversion:", result2)
print("Type of result:", type(result2))
Result after conversion: 98 Type of result: <class 'int'>
Common Conversion Functions
Python provides built-in functions for data type conversion ?
| Function | Purpose | Example |
|---|---|---|
int(x) |
Converts to integer | int("123") |
float(x) |
Converts to float | float("12.5") |
str(x) |
Converts to string | str(123) |
list(x) |
Converts to list | list("abc") |
tuple(x) |
Converts to tuple | tuple([1,2,3]) |
bool(x) |
Converts to boolean | bool(1) |
String to Integer Conversion
The int() function converts strings to integers. It accepts an optional base parameter for different number systems ?
a = "58"
print("Before conversion:", type(a))
number = int(a)
print("After conversion:", type(number))
print("Value:", number)
Before conversion: <class 'str'> After conversion: <class 'int'> Value: 58
String to Float Conversion
The float() function converts strings or numbers to floating-point values ?
a = "84.5"
print("Before conversion:", type(a), "Value:", a)
number = float(a)
print("After conversion:", type(number), "Value:", number)
Before conversion: <class 'str'> Value: 84.5 After conversion: <class 'float'> Value: 84.5
Character and Number Conversions
Character to Unicode using ord()
char = "H"
unicode_value = ord(char)
print(f"Unicode of '{char}' is {unicode_value}")
Unicode of 'H' is 72
Number to Hexadecimal using hex()
x = 87
hex_value = hex(x)
print(f"{hex_value} is of type {type(hex_value)}")
0x57 is of type <class 'str'>
Collection Conversions
String to List
text = "Python"
char_list = list(text)
print("String to list:", char_list)
String to list: ['P', 'y', 't', 'h', 'o', 'n']
String to Tuple
text = "Python"
char_tuple = tuple(text)
print("String to tuple:", char_tuple)
String to tuple: ('P', 'y', 't', 'h', 'o', 'n')
String to Set
text = "Hello"
char_set = set(text)
print("String to set:", char_set)
String to set: {'e', 'H', 'o', 'l'}
Conclusion
Python provides comprehensive data conversion capabilities through implicit conversion for safety and explicit conversion functions for precise control. Use int(), float(), str() for basic conversions, and collection functions like list(), tuple(), set() for data structure transformations.
