Python - Type Conversion

4 minute read

Type conversion in Python, also known as type casting, is the process of changing the data type of a variable from one type to another. Python provides several built-in functions for performing type conversion. In this tutorial, we’ll go over the various type conversion functions and how to use them with examples.

Implicit Type Conversion

Before we dive into the explicit type conversion functions, it’s worth noting that Python also supports implicit type conversion. This means that Python can automatically convert variables from one type to another as needed. For example, if you try to add an integer and a float, Python will automatically convert the integer to a float before performing the addition.

Here’s an example of implicit type conversion:

x = 10
y = 3.14
z = x + y

print(z) # Output: 13.14

In this example, Python converts the integer 'x' to a float before adding it to the float 'y'. The resulting value, '13.14', is also a float.

While implicit type conversion can be convenient, it’s important to be aware of it and to ensure that the resulting type is what you expect.

Explicit Type Conversion

Explicit type conversion involves using a built-in function to change the type of a variable. There are several type conversion functions available in Python, including:

  • 'int()' - converts a value to an integer
  • 'float()' - converts a value to a float
  • 'str()' - converts a value to a string
  • 'bool()' - converts a value to a boolean
  • 'list()' - converts a value to a list
  • 'tuple()' - converts a value to a tuple
  • 'set()' - converts a value to a set

Converting to an Integer

The 'int()' function can be used to convert a value to an integer. If the value is a floating-point number, the function will round down to the nearest integer.

Here are some examples:

x = int(3.14)
y = int("42")
z = int(True)

print(x) # Output: 3
print(y) # Output: 42
print(z) # Output: 1

In the first example, 'int(3.14)' converts the float '3.14' to an integer, resulting in '3'.

In the second example, int("42") converts the string '"42"' to an integer, resulting in '42'.

In the third example, 'int(True)' converts the boolean 'True' to an integer, resulting in '1'.

Converting to a Float

The 'float()' function can be used to convert a value to a float. If the value is an integer, the function will add a decimal point and a zero after the decimal point.

Here are some examples:

x = float(42)
y = float("3.14")
z = float(True)

print(x) # Output: 42.0
print(y) # Output: 3.14
print(z) # Output: 1.0

In the first example, 'float(42)' converts the integer '42' to a float, resulting in '42.0'.

In the second example, 'float("3.14")' converts the string '"3.14"' to a float, resulting in '3.14'.

In the third example, 'float(True)' converts the boolean 'True' to a float, resulting in '1.0'.

Converting to a String

The 'str()' function can be used to convert a value to a string. This is often useful when working with input/output operations or when concatenating strings with other data types.

Here are some examples:

x = str(42)
y = str(3.14)
z = str(True)

print(x) # Output: "42"
print(y) # Output: "3.14"
print(z) # Output: "True"

In the first example, 'str(42)' converts the integer '42' to a string, resulting in the string '"42"'.

In the second example, 'str(3.14)' converts the float '3.14' to a string, resulting in the string '"3.14"'.

In the third example, 'str(True)' converts the boolean 'True' to a string, resulting in the string '"True"'.

Converting to a Boolean

The 'bool()' function can be used to convert a value to a boolean. Any non-zero value will be converted to 'True', while '0', '0.0', 'None', and empty sequences (e.g. '""', '[]', '()') will be converted to 'False'.

Here are some examples:

x = bool(42)
y = bool(0)
z = bool("hello")

print(x) # Output: True
print(y) # Output: False
print(z) # Output: True

In the first example, 'bool(42)' converts the integer '42' to 'True'.

In the second example, 'bool(0)' converts the integer '0' to 'False'.

In the third example, 'bool("hello")' converts the string '"hello"' to 'True'.

Converting to a List, Tuple, or Set

The 'list()', 'tuple()', and 'set()' functions can be used to convert a value to a list, tuple, or set, respectively.

Here are some examples:

x = list("hello")
y = tuple([1, 2, 3])
z = set((4, 5, 6))

print(x) # Output: ['h', 'e', 'l', 'l', 'o']
print(y) # Output: (1, 2, 3)
print(z) # Output: {4, 5, 6}

In the first example, 'list("hello")' converts the string '"hello"' to a list of characters, resulting in '['h', 'e', 'l', 'l', 'o']'.

In the second example, 'tuple([1, 2, 3])' converts the list '[1, 2, 3]' to a tuple, resulting in '(1, 2, 3)'.

In the third example, 'set((4, 5, 6))' converts the tuple '(4, 5, 6)' to a set, resulting in '{4, 5, 6}'.

Conclusion

Type conversion is an important aspect of Python programming, as it allows you to change the data type of a variable as needed. Python provides several built-in functions for performing type conversion, including 'int()', 'float()', 'str()', 'bool()', 'list()', 'tuple()', and 'set()'. Understanding how to use these functions will enable you to write more flexible and robust code. Remember to be aware of implicit type conversion and to use explicit type conversion when necessary to ensure that your code behaves as expected.

Updated: