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.
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))
EOSCommand:
python3 s.pyRecorded output:
-1 ['a', 'b-c'] a_bXExit 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'))
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:
ValueError: substring not foundExit 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.
Limits
strip(chars) is a character set, not a literal prefix or suffix.
Source
Evidence
TE-39-A: string methods return new values; strip with a character set is not prefix removalTE-39-B: find returns -1 for a missing text, split can stop after one cut, and replace can be limited to one changeTE-39-C: expected non-zero exit: index raises ValueError for a missing text where find returned -1
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
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