Type Conversion in Python

In Python, type conversion (also known as type casting) refers to the process of converting one data type into another. Python provides built-in functions to perform type conversion, allowing you to convert variables or values from one data type to another as needed.

There are two types of type conversion in Python:

  1. Implicit type conversion: This type of conversion is performed automatically by the Python interpreter. For example, if you add an integer and a string, the Python interpreter will automatically convert the string to an integer before performing the addition.
  2. Explicit type conversion: This type of conversion is performed by the user using the built-in type conversion functions. For example, the int() function can be used to convert a string to an integer.

Here are some common type conversion functions along with examples:

int() function

Converts a value to an integer data type.

# Convert a string to an integer string_num = "10" integer_num = int(string_num) print(integer_num) # Output: 10
# Convert a float to an integer float_num = 3.14 integer_num = int(float_num) print(integer_num) # Output: 3

float() function

Converts a value to a floating-point data type.

# Convert an integer to a float integer_num = 10 float_num = float(integer_num) print(float_num) # Output: 10.0
# Convert a string to a float string_num = "3.14" float_num = float(string_num) print(float_num) # Output: 3.14

str() function

Converts a value to a string data type.

# Convert an integer to a string integer_num = 100 string_num = str(integer_num) print(string_num) # Output: "100"
# Convert a float to a string float_num = 3.14 string_num = str(float_num) print(string_num) # Output: "3.14"

bool() function

Converts a value to a Boolean data type.

# Convert an integer to a boolean integer_num = 0 boolean_val = bool(integer_num) print(boolean_val) # Output: False
# Convert a non-empty string to a boolean non_empty_string = "Hello" boolean_val = bool(non_empty_string) print(boolean_val) # Output: True
# Convert an empty string to a boolean empty_string = "" boolean_val = bool(empty_string) print(boolean_val) # Output: False

list(), tuple(), set() functions

Convert a sequence or iterable to a list, tuple, or set, respectively.

# Convert a string to a list of characters string_val = "Hello" char_list = list(string_val) print(char_list) # Output: ['H', 'e', 'l', 'l', 'o']
# Convert a list to a tuple numbers_list = [1, 2, 3, 4] numbers_tuple = tuple(numbers_list) print(numbers_tuple) # Output: (1, 2, 3, 4)
# Convert a list to a set colors_list = ['red', 'green', 'blue', 'green'] colors_set = set(colors_list) print(colors_set) # Output: {'blue', 'green', 'red'}

Python string to int

Conclusion

Type conversion is essential when you need to perform operations or utilize variables of different data types. It allows you to manipulate and combine data of various types, enhancing the flexibility and versatility of your Python programs. However, be cautious when converting data types, as some conversions may lead to loss of information or unexpected results.