Quick Answer

Fix TypeError when combining strings and numbers.

Understanding the Issue

Python doesn't allow direct concatenation of strings and numbers. You must explicitly convert numbers to strings using str() or use formatted string literals (f-strings). This prevents accidental type mismatches.

The Problem

This code demonstrates the issue:

Python Error
age = 25
message = "I am " + age + " years old"  # Fails

The Solution

Here's the corrected code:

Python Fixed
# Solution 1: str() conversion
message = "I am " + str(age) + " years old"

# Solution 2: f-strings (Python 3.6+)
message = f"I am {age} years old"

# Solution 3: format() method
message = "I am {} years old".format(age)

Key Takeaways

Always convert numbers to strings before concatenation.