Code with the error

apples = 5
print("Apples: " + apples)

Error message (Python 3.12, last line)

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

Corrected code

apples = 5
print("Apples: " + str(apples))

Output

Apples: 5

Common causes

  • Text and a number are joined with +.
  • A function gets too many or too few arguments.
  • Brackets after something that is not a function, such as a number.
  • Indexing a value that has no items, for example None or a number.

Practise in the course

Lesson: Strings

Frequently asked questions

How do I find out the type of a value?

Print type(value): Python shows int, str, list or another type.

Is there a way to join text and numbers without str()?

Yes, an f-string does it: f"Apples: {apples}".