python type_error ai_generated true

TypeError: unsupported operand type(s) for +: 'int' and 'str'

ID: python/typeerror-unsupported-operand

Also available as: JSON · Markdown
95%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
311 active

Root Cause

Binary operation between incompatible types. Common: concatenating str + int without conversion.

generic

Workarounds

  1. 95% success Explicitly convert types: str(num) for concat, int(s) for math
    result = 'Count: ' + str(count)  # or f'Count: {count}'

    Sources: https://docs.python.org/3/library/stdtypes.html#str

  2. 95% success Use f-strings for mixed type formatting: f'{name}: {value}'
    f'{name}: {value}'

    Sources: https://docs.python.org/3/reference/lexical_analysis.html#f-strings

Dead Ends

Common approaches that don't work:

  1. Convert everything to str 60% fail

    May hide a logic error — you might want numeric addition, not concatenation

  2. Use eval() to auto-detect types 90% fail

    eval is a security risk and doesn't fix type issues

Error Chain

Frequently confused with: