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('.')
'0.0'

should be equivalent to the solution I found later

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

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')
'ssiss'

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@python
components: Documentation
messages: 243283
nosy: PhoenixofMT, docs@python
priority: normal
severity: normal
status: open
title: string.strip() documentation is misleading
type: behavior
versions: Python 3.4

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24204>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to