8

I tried using rstrip() and lstrip() like so:

>>> a = 'thisthat'
>>> a.lstrip('this')
'at'
>>> a.rstrip('hat')
'this'
>>> 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).

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
sans0909
  • 395
  • 7
  • 20

5 Answers5

12

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).

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
3
a = 'thisthat'    
a.rstrip('hat')

is equivalent to

a = 'thisthat' 
to_strip = 'hat'
while a[-1] in to_strip:
    a = a[:-1]
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
宏杰李
  • 11,820
  • 2
  • 28
  • 35
1

strip() can remove all combinations of the spcific characters (spaces by default) from the left and right sides of string.

lstrip() can remove all combinations of the spcific characters (spaces by default) from the left sides of string.

rstrip() can remove all combinations of the spcific characters (spaces by default) from the right sides of string.

test = "    a b c    "

print(test)          # "    a b c    "
print(test.strip())  # "a b c"
print(test.lstrip()) # "a b c    "
print(test.rstrip()) # "    a b c"
test = "abc a b c abc"

print(test)               # "abc a b c abc"
print(test.strip("cab"))  # " a b c "
print(test.lstrip("cab")) # " a b c abc"
print(test.rstrip("cab")) # "abc a b c "
test = "abc a b c abc"

print(test)               # "abc a b c abc"
print(test.strip("c ab"))  # ""
print(test.lstrip("c ab")) # ""
print(test.rstrip("c ab")) # ""
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
-1

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'
B Rad C
  • 510
  • 2
  • 6
  • 18
  • @B Rad C No, I'm not looking for replace, I thought when i strip a word from the string it will take that substring and strips it off. I didn't know it takes chars as long as it can as Ahsanul Haque explained. – sans0909 Dec 06 '16 at 08:34
-1

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
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153