Python Error

Fix: TypeError: can only concatenate str (not 'int') to str

Exact message

TypeError: can only concatenate str (not "int") to str

Quick answer

This TypeError occurs when you use the + operator between a string and an integer in Python. Python does not auto-convert types, so you must wrap the integer with str(), or use an f-string for implicit conversion.

Why this error happens

Python's + operator behaves differently for strings and integers: for strings it concatenates, for integers it adds. Mixing the two raises TypeError because Python refuses to guess whether you want '12' or 3 from '1' + 2. Unlike JavaScript, Python never silently coerces types.

Example: Wrong vs Right

Wrong

total = "Total: " + 100
# TypeError: can only concatenate str (not "int") to str

Right

# Option 1: explicit str()
total = "Total: " + str(100)

# Option 2: f-string (recommended)
total = f"Total: {100}"

# Option 3: format()
total = "Total: {}".format(100)

How to fix it (step by step)

  1. Find the line where a string is added to a non-string with +
  2. Wrap the non-string value with str() or convert it inside an f-string
  3. If you are sure you wanted numeric addition, remove the quotes from the string
  4. Test with print(type(...)) before the + to verify both operands are strings

Frequently asked questions

Why does Python not auto-convert int to str?

Python is strongly typed by design. Auto-conversion hides bugs, so Python forces you to be explicit with str() or f-strings.

What is the fastest way to fix this error?

Use an f-string: f"Total: {100}". It is faster than str() and more readable.

Does the same error occur with floats?

Yes. Any non-str type (int, float, bool, list) mixed with a string using + raises this TypeError.

Related Python errors

Need a fix for a different error?

Use the free to paste any traceback and get an AI-suggested fix in seconds.

Want an AI with memory for your project?

MonsterX AI remembers your codebase across chats.