Type Conversion in Python
Type conversion, or casting, in Python allows you to change a variable from one type to another. This operation is common when you want to manipulate data in different formats.
Conversion Between Numeric Types
Conversion Between int, float, and complex
Python allows flexible conversion between numeric types:
- int to float: Convert an integer to a floating-point number.
- float to int: Convert a floating-point number to an integer, which truncates the decimal part.
- int to complex: Convert an integer to a complex number with zero imaginary part.
- float to complex: Convert a floating-point number to a complex number with zero imaginary part.
- complex to int: Direct conversion from complex to integer is not possible because it would lose the imaginary part. The real part must be extracted first.
- complex to float: Direct conversion from complex to float is not possible because it would lose the imaginary part. The real part must be extracted first.
Examples
# Convert int to float int_value = 42 float_value = float(int_value) print(float_value) # Output: 42.0 # Convert float to int float_value = 42.7 int_value = int(float_value) print(int_value) # Output: 42 (decimal part is truncated) # Convert int to complex int_value = 42 complex_value = complex(int_value) print(complex_value) # Output: (42+0j) # Convert float to complex float_value = 42.7 complex_value = complex(float_value) print(complex_value) # Output: (42.7+0j) # Convert complex to float (only real part is considered) complex_value = 42 + 3j real_part = float(complex_value.real) print(real_part) # Output: 42.0
Conversion Between str and Numeric Types
Strings (str) can be converted to integers, floats, or complex numbers, and vice versa.
- str to int: Convert a string representing an integer to an int.
- str to float: Convert a string representing a float to a float.
- str to complex: Convert a string representing a complex number to a complex.
Examples
# Convert string to int str_value = "123" int_value = int(str_value) print(int_value) # Output: 123 # Convert string to float str_value = "123.45" float_value = float(str_value) print(float_value) # Output: 123.45 # Convert string to complex str_value = "1+2j" complex_value = complex(str_value) print(complex_value) # Output: (1+2j)
Conversion Between Other Types
Python also allows conversion between other data types such as strings, lists, sets, and tuples.
Conversion Between str and list, set, tuple
- str to list: Convert a string to a list of characters.
- list to str: Convert a list of characters to a string.
- str to set: Convert a string to a set of unique characters.
- set to str: Convert a set of characters to a string.
- str to tuple: Convert a string to a tuple of characters.
- tuple to str: Convert a tuple of characters to a string.
Examples
# Convert string to list str_value = "hello" list_value = list(str_value) print(list_value) # Output: ['h', 'e', 'l', 'l', 'o'] # Convert list to string list_value = ['h', 'e', 'l', 'l', 'o'] str_value = ''.join(list_value) print(str_value) # Output: hello # Convert string to set str_value = "hello" set_value = set(str_value) print(set_value) # Output: {'h', 'e', 'l', 'o'} # Convert set to string set_value = {'h', 'e', 'l', 'o'} str_value = ''.join(set_value) print(str_value) # Output: (order of characters may vary) # Convert string to tuple str_value = "hello" tuple_value = tuple(str_value) print(tuple_value) # Output: ('h', 'e', 'l', 'l', 'o') # Convert tuple to string tuple_value = ('h', 'e', 'l', 'l', 'o') str_value = ''.join(tuple_value) print(str_value) # Output: hello
Conversion Between list and set, tuple
- list to set: Convert a list to a set, removing duplicates.
- set to list: Convert a set to a list.
- list to tuple: Convert a list to a tuple.
- tuple to list: Convert a tuple to a list.
Examples
# Convert list to set list_value = [1, 2, 2, 3, 4] set_value = set(list_value) print(set_value) # Output: {1, 2, 3, 4} # Convert set to list set_value = {1, 2, 3, 4} list_value = list(set_value) print(list_value) # Output: [1, 2, 3, 4] (order may vary) # Convert list to tuple list_value = [1, 2, 3, 4] tuple_value = tuple(list_value) print(tuple_value) # Output: (1, 2, 3, 4) # Convert tuple to list tuple_value = (1, 2, 3, 4) list_value = list(tuple_value) print(list_value) # Output: [1, 2, 3, 4]
Conversion Errors
Some conversions can fail if the data is not in the expected format. For example, attempting to convert a non-numeric string to an integer or float will raise an error.
Example of Error
# Attempt to convert a non-numeric string str_value = "abc" try: int_value = int(str_value) # This will fail except ValueError as e: print(e) # Output: invalid literal for int() with base 10: 'abc'
Custom Conversion
You can also define custom conversion functions to handle specific formats or requirements.
Example of Custom Conversion
def convert_to_complex(real_str, imag_str): return complex(float(real_str), float(imag_str)) real_part = "5.0" imaginary_part = "3.0" complex_value = convert_to_complex(real_part, imaginary_part) print(complex_value) # Output: (5+3j)