[New-bugs-announce] [issue24204] string.strip() documentation is misleading

Jim report at bugs.python.org
Fri May 15 22:49:36 CEST 2015

New submission from Jim:

This probably applies to all versions with the strip() method, but I'm using 3.4. Section 4.7.1 of the documentation has a poorly worded description/example for the behaviour of string.strip([chars]).

A casual reading of "The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped" lead me to believe this

>>> '0.0'.strip('.')

should be equivalent to the solution I found later

>>> '0.0'.replace('.', '')

The bit about [chars] requires recursive thinking ("are _stripped_") and clouds the fact that strip() iterates from beginning and end, discarding characters until it reaches a character that isn't in [chars].

In the example, it's not obvious (or wasn't to me) that the 'm' wasn't removed from 'example', and the missing periods gave the impression that they had been removed from the middle of the string instead of iterated to from the ends.

I can't think of a good way to rewrite the description, but perhaps you could borrow an example from rstrip() and add/replace:

>>> 'mississippi'.strip('mip')

The glaring existence of that 'i' in the middle, when all others have been removed makes the limitation clear.

>>> '    hello world    '.strip()
'hello world'

Makes another good example.

Just trying to save someone else 20 minutes of confusion.

assignee: docs at python
components: Documentation
messages: 243283
nosy: PhoenixofMT, docs at python
priority: normal
severity: normal
status: open
title: string.strip() documentation is misleading
type: behavior
versions: Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list