This is Python 3.4.2

The documentation says that

s2 = s1.strip('a')

will strip out all the 'a' in s1.

When I run it, only the leftmost character - and only if it is 'a' - is stripped.

The default s2 = s1.strip() takes out all whitespace.
PS: I understand that the chars in () are a list.
This is from an IDLE shell:

s2 = 'qazxswedc'.rstrip('x')
s2
'qazxswedc'

(Also with .lstrip('x') and .strip('x') )

Have I found the very last bug in Python?

Recommended Answers

Standard builtin functions have been tested gazillion times by programs all around the world. It is almost impossible that you discover an unknown bug in them. strip() removes characters only at both ends of the string. You will have to use replace()

>>> 'qazxswedc'.replace('x', '')
'qazswedc'
Jump to Post

All 6 Replies

Re: strip() method with chars doesn't seem to work 80 80

Standard builtin functions have been tested gazillion times by programs all around the world. It is almost impossible that you discover an unknown bug in them. strip() removes characters only at both ends of the string. You will have to use replace()

>>> 'qazxswedc'.replace('x', '')
'qazswedc'
Re: strip() method with chars doesn't seem to work 80 80

From the Python manual that ships with every installation:
str.strip([chars])
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. ...

Python has an easy syntax, the trick is to get to know the many functions/methods that it offers. Good luck with your studies.

Re: strip() method with chars doesn't seem to work 80 80

Ooops. I used a few unfortunate test cases. I also thought that strip took out all whitespace.

Now I get the difference between strip() and replace().

Thank you all for your help.

Re: strip() method with chars doesn't seem to work 80 80

Most of the time strip() is used to remove whitespaces at either end of a string.

rstrip() is handy for removing trailing (to the right) newline characters that might come in on a multiline text string.

The Python Shell makes it very easy to test these things.

Re: strip() method with chars doesn't seem to work 80 80

Similiar to ZZMike, I too have encountered what appears to be a bug with the Python strip() method. As suggested futher on in the post, the replace () method works as expected:

LAPTOP-MOQUDB6E:/home/bluenunn/python/data $ python3
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>>
>>> world = "     earth     "
>>>
>>> world.strip('e')
'      earth     '
>>>
>>> world.replace('e', '')
'     arth     '
Re: strip() method with chars doesn't seem to work 80 80

@bluenunn - If you want to add to an existing post that is obviously dead, it is better to add a new question and reference the old one in the question.

That being said, what is the bug? e is not at the beginning or the end of the string. Why would you expect strip to remove it? I think perhaps you might need to read the docs again or follow a tutorial on the subject.

If, however, you want the whitespace and the e stripped, you can use this, world.strip('e ')

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.