Type Casting

Difficulty: Beginner

Type casting (also called type conversion) is the process of converting a value from one data type to another. Python provides built-in functions for this purpose: int(), float(), str(), and bool(). These are essential for handling user input, performing calculations with mixed types, and formatting output.

The int() function converts a value to an integer. It can convert floats (by truncating the decimal part, not rounding), numeric strings, and booleans. For floats, int(3.9) gives 3, not 4 -- it always truncates toward zero. For strings, the string must contain a valid integer representation; int("3.14") will raise a ValueError because the string contains a decimal point.

The float() function converts a value to a floating-point number. It can handle integers, numeric strings (including those with decimal points), and booleans. This is commonly used when you need decimal precision in calculations, such as when computing averages or performing division.

The str() function converts any value to its string representation. This is frequently used to concatenate numbers with text, since Python does not allow adding a string and a number directly. For example, "Age: " + str(25) produces "Age: 25". The str() function works on virtually any Python object.

The bool() function converts a value to True or False. In Python, several values are considered "falsy" (they convert to False): 0, 0.0, empty string "", None, empty collections ([], {}, set()). Everything else is "truthy" and converts to True. Understanding truthiness is critical for writing clean conditional logic in Python.

Code examples

Converting with int()

print(int(3.7))
print(int(3.2))
print(int(-3.9))
print(int("42"))
print(int(True))
print(int(False))

int() truncates floats toward zero (not rounding). It can parse integer strings and convert booleans (True=1, False=0). Note that int(-3.9) gives -3, not -4.

Converting with float() and str()

print(float(10))
print(float("3.14"))
print(float(True))

print(str(42))
print(str(3.14))
print(str(True))

float() adds a decimal component to integers and parses numeric strings. str() converts any value to its text representation, which is what you would see if you printed the value directly.

Understanding bool() and Truthiness

print(bool(1))
print(bool(0))
print(bool(-5))
print(bool(""))
print(bool("hello"))
print(bool(0.0))
print(bool(3.14))

Zero values (0, 0.0) and empty strings are falsy. Any non-zero number or non-empty string is truthy. This is foundational for Python conditionals.

Practical: Processing User Input

# Simulating user input (input() always returns a string)
user_input = "25"
age = int(user_input)
double_age = age * 2
print("Double your age is " + str(double_age))

A typical workflow: receive string input, convert to int for math, then convert back to str for concatenation. This pattern appears constantly in real Python programs.

Key points

Concepts covered

int(), float(), str(), bool(), Type conversion