codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

9 / 68 topics
7Python Data Types Overview8Python Numbers & Math9Python Type Conversion (Casting)10Python Booleans & None11Python Strings & Methods12Python String Formatting13Python Operators & Precedence
Tutorials/Python Programming/Python Type Conversion (Casting)
🐍Python Programming

Python Type Conversion (Casting)

Updated 2026-05-15
30 min read

Python Type Conversion (Casting)

In Python, type conversion refers to changing the data type of a variable from one form to another. This is essential for ensuring that operations are performed correctly and efficiently. Understanding both implicit and explicit type conversions will help you write more robust and error-free code.

Introduction

Python supports two types of type conversions:

  1. Implicit Type Conversion (Automatic Conversion): Python automatically converts one data type to another when the situation demands it.
  2. Explicit Type Conversion (Manual Conversion): You manually convert a variable from one data type to another using built-in functions like int(), float(), str(), bool(), list(), tuple(), and set().

Knowing how these conversions work will help you handle different types of data effectively in your programs.

Implicit Type Conversion

Implicit type conversion occurs when Python automatically changes the data type of a variable to avoid errors during operations. This is usually done between compatible data types.

Example 1: Arithmetic Operations

In arithmetic operations, Python converts integers to floats if one of the operands is a float.

implicit_conversion.py
1# Implicit conversion in arithmetic operations
2a = 5
3b = 3.2
4result = a + b
5print(result) # Output: 8.2
Output
8.2

Here, the integer a is automatically converted to a float before performing addition with b.

Example 2: String Concatenation

When concatenating a string with a non-string type, Python converts the non-string to a string.

implicit_conversion.py
1# Implicit conversion in string concatenation
2name = "Alice"
3age = 30
4message = name + " is " + str(age) + " years old."
5print(message)
Output
Alice is 30 years old.

In this example, the integer age is automatically converted to a string.

Explicit Type Conversion

Explicit type conversion involves manually changing the data type of a variable using built-in functions. This is necessary when you want to ensure that a variable is of a specific type or when implicit conversion is not possible.

Common Conversion Functions

Here are some common functions used for explicit type conversion:

  • 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.

Example 1: Converting Strings to Numbers

You can convert strings that represent numbers into integers or floats using the int() and float() functions.

explicit_conversion.py
1# Converting strings to numbers
2num_str = "42"
3print(int(num_str)) # Output: 42
4print(float(num_str)) # Output: 42.0
Output
42
42.0

Example 2: Converting Numbers to Strings

You can convert numbers to strings using the str() function.

explicit_conversion.py
1# Converting numbers to strings
2num = 3.14
3print(str(num)) # Output: "3.14"
Output
"3.14"

Example 3: Converting Booleans

You can convert booleans to integers (True becomes 1, False becomes 0) and strings ("True" or "False").

explicit_conversion.py
1# Converting booleans
2is_valid = True
3print(int(is_valid)) # Output: 1
4print(str(is_valid)) # Output: "True"
Output
1
True

Example 4: Converting Lists, Tuples, and Sets

You can convert between lists, tuples, and sets using the list(), tuple(), and set() functions.

explicit_conversion.py
1# Converting lists, tuples, and sets
2my_list = [1, 2, 3]
3my_tuple = tuple(my_list)
4print(my_tuple) # Output: (1, 2, 3)
5
6my_set = set(my_tuple)
7print(my_set) # Output: {1, 2, 3}
Output
(1, 2, 3)
{1, 2, 3}

Practical Example

Let's create a mini-program that takes user input, converts it to the appropriate data type, and performs some operations.

practical_example.py
1# Mini-program: User Input and Type Conversion
2name = input("Enter your name: ")
3age_str = input("Enter your age: ")
4
5# Convert age from string to integer
6age = int(age_str)
7
8# Calculate the year of birth
9current_year = 2023
10year_of_birth = current_year - age
11
12print(f"Hello, {name}! You were born in {year_of_birth}.")
Terminal
$ python3 practical_example.py
Enter your name: Alice
Enter your age: 30
Hello, Alice! You were born in 1993.

In this program, the user's input for age is initially a string. We convert it to an integer using int() before performing arithmetic operations.

Summary

  • Implicit Type Conversion: Python automatically converts data types when necessary.
  • Explicit Type Conversion: You manually convert data types using built-in functions like int(), float(), str(), bool(), list(), tuple(), and set().
  • Understanding type conversion is crucial for writing robust and error-free Python code.

What's Next?

In the next topic, we will explore Python Booleans & None, where you'll learn about boolean values, truthy and falsy values, and the special None type. This knowledge will help you make decisions in your programs using conditions and control structures.

Stay tuned for more Python tutorials!


PreviousPython Numbers & MathNext Python Booleans & None

Recommended Gear

Python Numbers & MathPython Booleans & None