strip, lstrip and rstrip in python

In this article we are going to see the following three methods. They are

  1. strip
  2. lstrip
  3. rstrip

These three methods are the string formatting methods. They are used to remove characters from the string. These methods will take the characters to be removed as the arguments.

strip

The strip method removes the characters from the beginning or at the end. If no characters are passed as the arguments then the white spaces are removed from both the ends. The strip method can be easily explained with the following examples.

removing whitespaces from both the ends
strip with arguments

In this example, we have given the letter ‘b‘ as the argument to the strip method. Hence ‘b’ from the word blob is removed.

In this example the letter ‘bl‘ matches only at the beginning and hence the letters from the beginning alone is removed. Whereas in the second example the letters ‘rry‘ matches only at the end and hence the letters at the end are removed.

lstrip

The lstrip() method removes from the beginning only and not from the trailing end. This will come in handy when you do not want letters or characters to be removed from the trailing end.

lstrip method in python

rstrip

The rstrip() method is the opposite of the lstrip() method. The rstrip() method removes the trailing characters only and not from the beginning. This will come in handy when you do not want letters or characters to be removed from the beginning but only from the end.

rstrip method in python

Hope this article was helpful.

Happy coding !