How do the .strip/.rstrip/.lstrip string methods work in Python?

Question:

I tried using .rstrip and .lstrip like so:

>>> a = 'thisthat'
>>> a.rstrip('hat')
'this'
>>> a.lstrip('this')
'at'
>>> a.rstrip('cat')
'thisth'

What exactly are these methods doing? I expected 'thist' for the first case and 'that' for the second case.

I’m not looking to fix the problem, I just want to understand the functionality.


See also How do I remove a substring from the end of a string? if you do want to fix a problem with removing something from the beginning or end of a string (or are trying to close such a duplicate question).

Asked By: sans0909

||

Answers:

From the documentation:

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. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

So, strip will try to remove any of the characters listed in chars from both ends as long as it can. That is, the string provided as an argument is considered as a set of characters, not as a substring.

lstrip and rstrip work the same way, except that lstrip only removes characters on the left (at the beginning) and rstrip only removes characters on the right (at the end).

Answered By: Ahsanul Haque
a = 'thisthat'    
a.rstrip('hat')

is equivalent to

a = 'thisthat' 
to_strip = 'hat'
while a[-1] in to_strip:
    a = a[:-1]
Answered By: 宏杰李

lstrip, rstrip and strip remove characters from the left, right and both ends of a string respectively. By default they remove whitespace characters (space, tabs, linebreaks, etc)

>>> a = '  string with spaces  '
>>> a.strip()
'string with spaces'
>>> a.lstrip()
'string with spaces  '
>>> a.rstrip()
'  string with spaces'

You can use the chars parameter to change the characters it strips.

>>> a = '....string....'
>>> a.strip('.')
'string'

However, based on your question it sounds like you are actually looking for replace

>>> a = 'thisthat'
>>> a.replace('hat', '')
'thist'
>>> a.replace('this', '')
'that'
Answered By: B Rad C

This is how I understand the lstrip and rstrip methods:

#USING RSTRIP
a = 'thisthat'
print(a.rstrip('hat'))
#Begin with t on the right side, is t in 'hat'? Okay, remove it
#Next: a, is a in 'hat'? Okay remove it
#Next: h, is h in 'hat'? Okay, remove it
#Next: t, is t in 'hat'? Okay, remove it
#Next: s, is s in 'hat'? No, so, stop.
#Output: this

#USING LSTRIP
b = 'thisthat'
print(b.lstrip('this')) 
#Begin with t on the left side, is t in 'this'? Okay, remove it
#Next: h, is h in 'this'? Okay, remove it
#Next: i, is i in 'this'? Okay,  remove it
#Next: s, is s in 'this'? Okay, remove it
#Next: t, is t in 'this'? Okay, remove it
#Next: h, is h in 'this'? Okay, remove it
#Next: a, is a in 'this'? No, so, stop
#Ouput: at

#Using STRIP
c = 'thisthat'
print(c.strip("th"))
#Begin from both sides and repeat the steps from above; essentially, stripping from both sides.
#Using lstrip => isthat
#Now, using rstrip => istha
#Ouput: istha
Answered By: Blue-Spartan
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.