PYTHON • Beginner

Python Type Casting

Type casting (or type conversion) is the process of converting a value from one data type to another. In Python, this is done using built-in constructor functions like int(), float(), str(), and bool().

What is Type Casting?

Sometimes you have data in one type but need it in another. For example, user input from input() always comes as a string, but if you want to do math with it, you need to convert it to int or float. Type casting solves this.

Two Types of Conversion

  • Implicit Casting — Python does it automatically (e.g., int + float = float).
  • Explicit Casting — You do it manually using functions like int(), str().

1. int() — Convert to Integer

# From float
print(int(3.9))        # 3 (truncates, does not round)

# From string
print(int("100"))      # 100

# From boolean
print(int(True))       # 1
print(int(False))      # 0

2. float() — Convert to Float

print(float(10))       # 10.0
print(float("3.14"))   # 3.14
print(float(True))     # 1.0

3. str() — Convert to String

print(str(100))        # "100"
print(str(3.14))       # "3.14"
print(str(True))       # "True"

# Useful for concatenation
age = 22
print("Age: " + str(age))   # Age: 22

4. bool() — Convert to Boolean

print(bool(1))         # True
print(bool(0))         # False
print(bool(""))        # False (empty string)
print(bool("Hi"))      # True
print(bool([]))        # False (empty list)
print(bool([1, 2]))    # True

5. list(), tuple(), set() — Convert Collections

# String to list
print(list("abc"))     # ['a', 'b', 'c']

# List to tuple
print(tuple([1, 2, 3]))    # (1, 2, 3)

# List to set (removes duplicates)
print(set([1, 2, 2, 3]))   # {1, 2, 3}

Real-World Example — User Input

This is the most common use of type casting in real projects:

# input() always returns a string
age = input("Enter your age: ")
age = int(age)         # Convert to int

# Now you can do math
next_year = age + 1
print("Next year you'll be", next_year)
⚠ Common Error

Trying to convert an invalid string raises a ValueError. For example, int("hello") throws an error because "hello" is not a number. Always validate user input.

💡 Academy Tip

Implicit casting example: print(10 + 3.5) returns 13.5 — Python automatically converts int to float. This is safe. But explicit casting is always recommended for clarity.

📝 Try Yourself

Ask the user for two numbers using input(). Convert both to float, then print their sum, difference, product, and division. Handle the case where the user enters invalid input.

Quick Revision

  • Type casting = converting one data type to another.
  • Use int(), float(), str(), bool().
  • int(3.9) → 3 (truncates, not rounds).
  • input() always returns a string — cast it for math.
  • Implicit casting happens automatically; explicit is manual.
  • Invalid conversions raise ValueError.

Interview Questions

Q: Difference between implicit and explicit type casting?

A: Implicit casting is done automatically by Python (e.g., 1 + 2.5 becomes 3.5). Explicit casting is when the programmer manually converts using int(), str(), etc. Explicit is safer and clearer.

Q: What does int("3.14") return?

A: It raises a ValueError because "3.14" is not a valid integer string. Use int(float("3.14")) first, then int().

Q: What happens when you do int(3.99)?

A: It returns 3, not 4. Python's int() truncates the decimal part. For rounding, use round(3.99) instead.

Want to Master Python?

Join our Python course in Varanasi — practical classes and career support at our Sarnath centre.

Ask on WhatsApp
Chat with us