Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

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

Verified FixPython 3.10+Python NativeSilo: core

Quick Fix / Solution Rapide

Python does not perform implicit type coercion during addition. Explicitly cast to string using str(value) for concatenation or to integer using int(value) for arithmetic.

Root Cause Analysis

This error occurs when Python tries to execute an addition operation between an integer and a string, but Python's strong typing system does not perform implicit type conversion between distinct data types.

Cause 1: Strong Dynamic Typing Mechanics

In weakly typed languages like JavaScript, evaluating 10 + '20' implicitly coerces numbers into strings, resulting in '1020'. Python deliberately rejects this ambiguity. Because + performs numerical addition on integers and concatenation on strings, Python cannot assume developer intent without explicit casting.

Cause 2: Uncoerced User and API Inputs

External sources—such as HTTP request bodies, environment variables via os.environ, or standard input via input()—always return string values. Adding a raw numeric string to an integer counter triggers TypeError: unsupported operand type(s) for +: 'int' and 'str'.

Cause 3: String Formatting and Concatenation

Constructing output strings using + concatenation instead of modern f-strings (f'id_{count}') frequently causes this error when integer IDs are mixed with string labels.

Reproduction Code (MCVE)

Example: Bug Reproduction
count = 10
suffix = '20'
total = count + suffix

Solution 1: Explicit String Conversion (str()) or f-Strings for Concatenation

When your objective is text concatenation, convert the integer to a string explicitly using str() or format strings with f'{count}{suffix}'.

Example: Recommended Solution
count = 10
suffix = '20'
result = f'{count}{suffix}'
print(f'Concatenated: {result}')

Solution 2: Explicit Integer Conversion (int()) for Arithmetic

When your objective is mathematical summation, parse the string into an integer using int() before evaluating the addition.

Example: Alternative Solution
count = 10
suffix = '20'
total = count + int(suffix)
print(f'Total: {total}')

Common Mistakes & Edge Cases

1. Contrasting TypeError vs ValueError

Adding mismatched types (10 + 'abc') raises TypeError at operand evaluation time. Attempting to parse non-numeric strings with int('abc') raises ValueError. Always guard string parsing with structured error handling.

2. The NoneType Addition Trap

If an API returns None instead of a string or integer, evaluating 10 + None raises TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'. Check for None before performing operations.

3. Collection Ingestion Boundaries

When processing records from CSV or JSON sources, ensure numeric fields are parsed once at the boundary rather than repetitively converting strings inside downstream calculation loops.