Search code examples
pythonstrip

Python string.rstrip() doesn't strip specified characters


string = "hi())("
string = string.rstrip("abcdefghijklmnoprstuwxyz")
print(string)

I want to remove every letter from given string using rstrip method, however it does not change the string in the slightest.

Output:

'hi())('

What i Want:

'())('

I know that I can use regex, but I really don't understand why it doesn't work.

Note : It is a part of the Valid Parentheses challenge on code-wars


Solution

  • You have to use lstrip instead of rstrip:

    >>> string = "hi())("
    >>> string = string.lstrip("abcdefghijklmnoprstuwxyz")
    >>> string
    '())('