Python str.join: build a string from a list
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.
TE-94-A: join with a separator, and a non-string item raises TypeError
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.py <<'EOS'
print(', '.join(['a', 'b', 'c']))
try:
'-'.join(['a', 1])
except TypeError:
print('TypeError')
EOSCommand:
python3 s.pyRecorded output:
a, b, c
TypeErrorExit 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):
cat > s.py <<'EOS'
print(repr(','.join([])), '-'.join('abc'), ','.join(['x']))
EOSCommand:
python3 s.pyRecorded output:
'' a-b-c xExit 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):
cat > s.py <<'EOS'
print(','.join([1, 2]))
EOSThe 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:
python3 s.py 2>../err.txt; rc=$?; tail -n 1 ../err.txt; exit $rcRecorded output:
TypeError: sequence item 0: expected str instance, int foundExit 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
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.
All full guides: All guides