Search code examples
pythonstrip

strip in Python 3 is not "working"?


I have the following string:

s = 'sd sdasd sas sas zxxx df xx de '

When I use s.strip('x')I get the following result:

'sd sdasd sas sas zxxx df xx de '

Why is strip() not removing all 'x' characters?


Solution

  • From Python 3.6 documentation:

    str.strip([chars]): Return a copy of the string with the leading and trailing characters removed.

    Which means that s.strip('x') removes only x from the start and from the end of the string (e.g. "xxxabcxx".strip('x') == "abc") If you need replace characters inside string you probably want to use the str.replace() method.