ProjectStack
python

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

You tried to join a string and a non-string value with the + operator. Python won't automatically convert numbers (or other types) to strings — you must do it explicitly.

Common causes

  • Using + to combine a string with an integer: 'Count: ' + 5
  • A variable holds a number but you assumed it was a string
  • Building a message string by concatenating a string with a function's return value that returns a number
  • Mixing data types when building SQL queries or log messages with +

How to fix it

  1. Convert the non-string value to a string: 'Count: ' + str(5)
  2. Use an f-string instead — cleaner and avoids the issue: f'Count: {count}'
  3. Use .format(): 'Count: {}'.format(count)
  4. Use a comma in print() to avoid concatenation entirely: print('Count:', count)

Example

Traceback (most recent call last): File "app.py", line 2, in <module> print('Score: ' + score) TypeError: can only concatenate str (not "int") to str

Concatenating a string literal with an integer variable using +

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.