# Python str.join: build a string from a list

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-str-join-full-en
`SEP.join(items)` joins the strings in `items` with `SEP` between them. Every item must be a string, or `join` raises `TypeError`.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-94-A, TE-94-B and TE-94-C. The expected non-zero exit is TE-94-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-94-A: join with a separator, and a non-string item raises TypeError

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
print(', '.join(['a', 'b', 'c']))
try:
    '-'.join(['a', 1])
except TypeError:
    print('TypeError')
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
a, b, c
TypeError
```

Exit status: 0. The output matches the expected text written before the run.

### TE-94-B: join on a one-item list adds no separator, on an empty list gives an empty string, and on a string joins its characters

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
print(repr(','.join([])), '-'.join('abc'), ','.join(['x']))
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
'' a-b-c x
```

Exit status: 0. The output matches the expected text written before the run.

### TE-94-C: expected non-zero exit: an uncaught TypeError from join prints which item was not a string

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
print(','.join([1, 2]))
EOS
```

The file `../err.txt` sits one level above the working folder, inside the same throwaway tree. The output block below shows only the last line of the error; the full traceback went to `../err.txt`.

Command:

```bash
python3 s.py 2>../err.txt; rc=$?; tail -n 1 ../err.txt; exit $rc
```

Recorded output:

```
TypeError: sequence item 0: expected str instance, int found
```

Exit status: 1. This status and the output match what was written before the run.

## Reading the recorded output

The first printed line, a, b, c, is the three strings joined with a comma and a space between them, and no separator after the last one. The second call used the one-character separator, a hyphen, on the list ['a', 1], which holds a text item and an integer. It raised an error, the except branch caught it, and the program printed the name TypeError. TE-94-A does not print the error message, so it shows that an error of that type happened and not what its text says. TE-94-C prints it.

TE-94-B: join on an empty list gave an empty string, shown by repr as two quote marks. join on the text abc with a hyphen gave a-b-c, and join on a one-item list gave x with no comma. TE-94-C: join on [1, 2] stopped with TypeError: sequence item 0: expected str instance, int found, with exit status 1. The message gives the position of the item and its type.

## Limits

The docs (https://docs.python.org/3/library/stdtypes.html#str.join) say in their str.join entry that a TypeError is raised when the iterable holds non-string values. This is a paraphrase, not a quotation. Only lists of strings, lists with integers, an empty list, a one-item list and a text were run.

## Source

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

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-94-A` | join with a separator, and a non-string item raises TypeError | 0 | PASS | `11c8a405dce6` |
| `TE-94-B` | join on a one-item list adds no separator, on an empty list gives an empty string, and on a string joins its characters | 0 | PASS | `8578cd7860d7` |
| `TE-94-C` | expected non-zero exit: an uncaught TypeError from join prints which item was not a string | 1 | PASS | `9a3160381c05` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python dict.get: read a key without KeyError](https://kairos-2z6s.onrender.com/res/python-dict-get-full-en)
- [Python lru_cache: remember function results](https://kairos-2z6s.onrender.com/res/python-lru-cache-full-en)
- [Reading a failed Python test, line by line](https://kairos-2z6s.onrender.com/res/unittest-failure-full-en)
- [Python string methods: strip, split, replace and friends](https://kairos-2z6s.onrender.com/res/python-string-methods-full-en)
- [Storing API keys in a Python project](https://kairos-2z6s.onrender.com/res/python-store-api-keys-env-vars-en)
- [Python subprocess.run: run a command, read output and exit code](https://kairos-2z6s.onrender.com/res/python-subprocess-full-en)
