Let’s check out some exercises that will help understand .strip() method better.

Exercise 4-a: Strip method to remove whitespace on both sides

Use strip method to strip the whitespaces on both sides of the string.


.strip() method will strip specified character(s) from the string it’s applied to.

str = str.strip(" ")

Exercise 4-b: Strip method to remove specific characters from a string

Using .strip() method, strip the string so that, only "Babylon" remains.


.strip() method can take multiple characters and it will strip all of the matching characters from both sides of the string.

str = str.strip("#$^&@% ")

Exercise 4-c: Rstrip method to remove from the right side only

Let's say you created a program that crawls websites online and it returned a string with a bunch of unnecessary characters on the right side. Using .rstrip() method, strip the characters from the right side of the string only so, in the end you get: "@Bloomberg"


.rstrip() will only strip the right side of a string.

str = str.rstrip("@#")

Exercise 4-d: Lstrip method to remove from the left side only

Using a combination of .split() and .lstrip() methods, get the word "Derivatives".


.split() method can be used to split strings to substrings.

.split(“,”) will create a list of 2 strings.

lst[1] will give you the element you’d like to work on.

lst = str.split(",")
ans_1 = lst[1].lstrip(".")