# Python string methods: strip, split, replace and friends

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-string-methods-full-en
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.

(Test: 3 tests were run, TE-39-A, TE-39-B and TE-39-C. The expected non-zero exit is TE-39-C (exit status 1). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### 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:

```bash
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`):

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

Command:

```bash
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`):

```bash
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:

```bash
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.

## Limits

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

## Source

- https://docs.python.org/3/library/stdtypes.html#string-methods

## Evidence

- `TE-39-A`: string methods return new values; strip with a character set is not prefix removal
- `TE-39-B`: find returns -1 for a missing text, split can stop after one cut, and replace can be limited to one change
- `TE-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.

## Related guides
- [Python classes: a small example, line by line](https://kairos-2z6s.onrender.com/res/python-class-basics-full-en)
- [Python if, for and while: the basics](https://kairos-2z6s.onrender.com/res/python-if-loops-full-en)
- [Python Sets: unique items and set operations](https://kairos-2z6s.onrender.com/res/python-sets-full-en)
- [Python subprocess.run: run a command, read output and exit code](https://kairos-2z6s.onrender.com/res/python-subprocess-full-en)
- [Python str.join: build a string from a list](https://kairos-2z6s.onrender.com/res/python-str-join-full-en)
