← Back to Chapters

Python Type Casting

? Python Type Casting

? Quick Overview

Type casting in Python means converting a value from one data type to another. It is very useful when working with different kinds of data, such as combining strings and numbers or making sure numeric calculations happen correctly.

Python supports:

  • Implicit casting – done automatically by Python when needed.
  • Explicit casting – done manually by the programmer using functions like int(), float(), str(), and bool().

? Key Concepts

  • Use casting to match types during arithmetic operations (e.g., int + float).
  • User input from input() is always a string and usually needs casting.
  • Casting prevents type errors when mixing strings and numbers in expressions.
  • Use casting to format data properly before saving to files or displaying to users.
  • Implicit casting usually happens from a smaller/less precise type to a more precise one (e.g., intfloat).
  • Explicit casting gives you full control over how and when conversion happens.

? Syntax and Theory

Common explicit casting functions in Python:

  • int(x) – converts x to an integer (decimals are truncated, not rounded).
  • float(x) – converts x to a floating-point number.
  • str(x) – converts x to a string.
  • bool(x) – converts x to a Boolean value (True or False).

When Python performs an arithmetic operation between an int and a float, it will implicitly convert the int to float so that precision is not lost.

? Code Examples

✨ Implicit Type Casting

In implicit casting, Python automatically converts one type to another during an operation.

? View Implicit Casting Example
x = 10        # int
y = 3.5       # float
z = x + y     # z becomes float (13.5)
print(z)
print(type(z))

?️ Explicit Type Casting

You can manually convert values to the type you need using casting functions.

? View Explicit Casting Example
x = int(3.9)         # x = 3
y = float("4.2")     # y = 4.2
z = str(100)         # z = "100"

print(x, y, z)

? Real-World Input Example

Most real programs take input from users as strings and then cast them before calculations.

? View User Input Casting Example
age = input("Enter your age: ")   # input returns string
age = int(age)                    # convert to integer
print("Next year, you will be", age + 1)

? Live Output / Explanation

? What These Examples Show

  • In the implicit casting example, x + y becomes 13.5 and type(z) is <class 'float'>, showing that Python upgraded int to float.
  • In the explicit casting example:
    • int(3.9) gives 3 (decimal part is removed).
    • float("4.2") converts the string "4.2" into a float value.
    • str(100) converts the number 100 into the string "100", which can be concatenated with other strings.
  • In the real-world example, input() returns a string. Using int(age) allows arithmetic like age + 1 to work correctly.

✅ Tips and Best Practices

  • Use casting to ensure data is always in the type your logic expects.
  • int() truncates decimals (e.g., int(3.9) becomes 3, not 4).
  • Always validate or handle errors for user input before casting from string.
  • When concatenating strings and numbers, convert numbers using str() or use f-strings, e.g., f"Age: {age}".
  • Be careful converting non-numeric strings like "abc" to int or float — they will raise a ValueError.

? Try It Yourself

  • Convert a float number to an integer using int() and print the result.
  • Take user input with input(), convert it into an integer using int(), and add 10 to it.
  • Concatenate a string and number by converting the number to string using str() or an f-string.
  • Experiment with bool() on values like 0, 1, empty strings "", and non-empty strings to see which are True or False.