Python Type Conversion

In Python, type conversion is the process of changing the data type of a value from one form to another—such as converting an integer to a string or a float to an integer. Python provides built-in functions to perform these conversions easily.

There are two main types of type conversion:

  • Explicit Conversion: When you manually convert data types using functions like int(), str(), and float().
  • Implicit Conversion: When Python automatically converts one data type to another during operations involving mixed types, such as adding an integer and a float.


📘 What You'll Learn

In this section, you will learn how to convert between different data types in Python using built-in functions. We’ll cover both explicit conversions—where you manually change types—and implicit conversions, where Python automatically handles type changes during operations.


Explicit Type Conversion

Explicit type conversion, also known as type casting, is when you manually convert a variable from one data type to another using Python's built-in functions. These include functions like int(), float(), str(), and bool().

✅ You should ensure the value is compatible with the target type — otherwise, Python will raise an error.

1. Converting from String to Integer

You can convert a string containing digits into an integer using the int() function. The string must be a valid whole number — it cannot contain letters, decimal points, or symbols.

python
string_value = "42"
integer_value = int(string_value)
print(integer_value)  # Output: 42

Invalid Example: This will raise an error because the string is not a valid integer:

python
string_value = "42abc"
integer_value = int(string_value)  # Raises ValueError

2. Converting from String to Float

The float() function can convert a string representing a decimal number into a float. The string must be properly formatted as a floating-point number.

python
string_value = "42.5"
float_value = float(string_value)
print(float_value)  # Output: 42.5

Invalid Example: This will raise an error:

python
string_value = "42.5kg"
float_value = float(string_value)  # Raises ValueError

3. Converting from Integer to String

Use str() to convert an integer into a string. This is useful when combining numbers with text.

python
integer_value = 42
string_value = str(integer_value)
print(string_value)  # Output: '42'

✅ This is a safe conversion and won’t raise an error. You can always convert any integer or float to a string.

4. Converting from Float to Integer

To convert a float to an integer, use int(). This will truncate the decimal part — it does not round to the nearest whole number.

python
float_value = 42.8
integer_value = int(float_value)
print(integer_value)  # Output: 42

Want rounding instead? Use the round() function:

python
float_value = 42.8
rounded_value = round(float_value)
print(rounded_value)  # Output: 43

5. Converting from Boolean to Integer

Booleans can also be converted to integers:True becomes 1, and False becomes 0.

python
print(int(True))   # Output: 1
print(int(False))  # Output: 0

Implicit Type Conversion

Implicit type conversion, also called type coercion, happens automatically when Python converts one data type to another in an expression. This usually occurs when mixing compatible numeric types — for example, an integer and a float.
Note: Python only performs implicit conversion between compatible types. It does not implicitly convert between strings and numbers.

Implicit Conversion Example: Integer to Float

When an integer and a float are used in a mathematical operation, Python implicitly converts the integer to a float to preserve precision:

python
integer_value = 5
float_value = 2.5
result = integer_value + float_value  # Implicit conversion to float
print(result)  # Output: 7.5

Implicit Conversion Example: Boolean to Integer

Booleans are treated as integers in arithmetic operations. True is converted to 1, and False to 0:

python
print(True + 3)    # Output: 4
print(False * 10)  # Output: 0

String and Integer: Not Implicitly Compatible

Python does not implicitly convert between strings and integers. You must explicitly convert the integer to a string using str() before concatenation:

python
integer_value = 42
string_value = " apples"
# Explicit conversion using str()
result = str(integer_value) + string_value
print(result)  # Output: '42 apples'

If you try to concatenate a string and an integer without conversion, you'll get a TypeError:

python
# This will raise an error
result = integer_value + " apples"  # TypeError: unsupported operand type(s)

📌 Frequently Asked Questions

What is type conversion in Python?

Type conversion means changing the data type of a value, like turning a string into an integer using int().


What is the difference between implicit and explicit type conversion?

Implicit conversion is automatic (e.g., adding int and float), while explicit conversion uses functions like float(), int(), or str().


How do I convert a string to an integer in Python?

Use int(). Example: int("42") converts the string "42" into the integer 42.


What happens if I convert a float to an integer?

Python will truncate the decimal, not round it. For instance, int(4.9) results in 4.


Can I convert a list to a tuple or vice versa?

Yes! Use tuple() to convert a list to a tuple, and list() to convert a tuple to a list.



🚀 What's Next?

Next, you'll learn about Python comments, which are essential for documenting and explaining your code. You'll discover how to use single-line and multi-line comments, as well as the importance of keeping your code readable and maintainable.