How to Fix: TypeError: sequence item 0: expected str instance, int found

3D isometric illustration of a robot failing to glue wooden string blocks to a metal integer block, next to a solution where the integer is converted to wood before joining, fixing the TypeError.

If you’ve encountered the TypeError sequence item expected str, you’re not alone. This error happens almost exclusively when using the .join() method.

The .join() method takes a list of strings and combines them. If the list contains anything else (like an integer), it crashes.

⚡ Quick Fix: TypeError: sequence item 0: expected str instance, int found – Python str.join() Integer to String Conversion Fix

You passed a list containing non-string items to .join() — convert every element to str first before the join runs.

my_numbers = [1, 2, 3]

# Fix 1 — List comprehension: readable and explicit
result = ", ".join([str(n) for n in my_numbers])
# Output: 1, 2, 3

# Fix 2 — map(): faster on large lists, no intermediate list built
result = ", ".join(map(str, my_numbers))
# Output: 1, 2, 3

# Fix 3 — Mixed list: convert everything to str regardless of type
result = ", ".join(str(item) for item in ["Alice", 30, True])
# Output: Alice, 30, True

This tells you exactly where your boundary is — now read through the root causes below to find which one broke your code and fix it permanently.

The Cause

You have a list of numbers, and you want to turn it into a string like "1, 2, 3".

Problem Code:

my_numbers = [1, 2, 3]

# CRASH!
result = ", ".join(my_numbers)
# TypeError: sequence item 0: expected str instance, int found

Python is saying: “I looked at item 0 (the number 1), and I expected a string, but I found an integer.”

The Fix: Convert to Strings First

You must convert every item in the list to a string before joining.

my_numbers = [1, 2, 3]

# Convert each item to str
string_list = [str(num) for num in my_numbers]

result = ", ".join(string_list)
print(result)
# Output: "1, 2, 3"

Fix 2: The map() Function (Faster for huge lists)

my_numbers = [1, 2, 3]

# map(str, list) applies str() to every item
result = ", ".join(map(str, my_numbers))

Use this whenever you need to format a list of data for printing or CSV export.


What This Error Exposes About Python’s str.join() Type Contract

TypeError: sequence item 0: expected str instance, int found is Python’s .join() implementation enforcing a homogeneous string contract at the element level. Before writing a single character to the output buffer, .join() inspects each item in the iterable and verifies it is a str instance. The moment it finds a non-string — an integer, a float, a boolean, a None — it raises the error at that item’s position and stops. No partial output is produced, no automatic conversion runs, and no fallback exists. The contract is total: every item must be a string, or the entire operation fails.

The list comprehension and map() patterns solve the problem at different points in the performance curve. A list comprehension [str(n) for n in my_numbers] builds a complete intermediate list of strings in memory before .join() runs — readable, debuggable, and correct for most use cases. map(str, my_numbers) produces a lazy iterator that converts each item on demand as .join() consumes it, avoiding the intermediate list allocation entirely. For lists with thousands or millions of items, map() keeps memory usage flat. For lists under a few thousand items, the difference is immeasurable and readability should win.

The mixed-type list scenario — ["Alice", 30, True, None] — is common in data pipelines assembling rows from heterogeneous sources before CSV export or log formatting. A generator expression (str(item) for item in row) handles every type in a single pass without requiring the list to be homogeneous. For production code that formats rows frequently, wrap the pattern in a utility function — def join_row(items, sep=", "): return sep.join(str(i) for i in items) — and call it consistently across your codebase rather than repeating the conversion logic at every call site.

Python Pro Hub readers also found these helpful:

Similar Posts

Leave a Reply