KKAIROS
Practical guideIllustrative4 min read

Python string methods: strip, split, replace and friends

String methods do not change the original string: they return a new value (a string, a list or a number), because strings are immutable.

Published:

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-39-A: string methods return new values; strip with a character set is not prefix removal

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

python3 -c "s='  Ab  '; print(repr(s.strip()), repr(s)); print(repr('xxAx'.strip('x')), repr('preAx'.removeprefix('pre')))"

Recorded output:

'Ab' '  Ab  '
'A' 'Ax'

This run checks the exit status and records the output; it asserts no expected text.

TE-39-B: find returns -1 for a missing text, split can stop after one cut, and replace can be limited to one change

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

cat > s.py <<'EOS'
print('abc'.find('z'), 'a-b-c'.split('-', 1), 'aXbX'.replace('X', '_', 1))
EOS

Command:

python3 s.py

Recorded output:

-1 ['a', 'b-c'] a_bX

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

TE-39-C: expected non-zero exit: index raises ValueError for a missing text where find returned -1

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

cat > s.py <<'EOS'
print('abc'.index('z'))
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:

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

Recorded output:

ValueError: substring not found

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

Reading the recorded output

The first line prints two values made with repr, so the quote marks and spaces show. s.strip() gave 'Ab', with the spaces at both ends removed. The variable s itself still printed as ' Ab ', with its spaces, so strip returned a new string and did not change the old one. In the second line, 'xxAx'.strip('x') gave 'A'. Both x characters at the start and the one at the end were removed, because the argument is a set of characters. 'preAx'.removeprefix('pre') gave 'Ax'.

TE-39-B: find returned -1 for a text that is not there, split with the limit 1 made one cut and gave ['a', 'b-c'], and replace with the count 1 changed only the first X, giving a_bX. TE-39-C: index on the same kind of search raised ValueError: substring not found, with exit status 1 and nothing else on standard output.

Ad

Limits

strip(chars) is a character set, not a literal prefix or suffix.

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-39-A | string methods return new values; strip with a character set is not prefix removal | 0 | PASS | c5157d4e799f | | TE-39-B | find returns -1 for a missing text, split can stop after one cut, and replace can be limited to one change | 0 | PASS | e3729d7fce15 | | TE-39-C | expected non-zero exit: index raises ValueError for a missing text where find returned -1 | 1 | PASS | 97de32cebab4 |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides