Strip with with lstrip(),rstrip() and strip() function in python

lstrip ()  function in python removes all the leading whitespaces or leading spaces of the string.

rstrip () function in python removes all the trailing whitespaces or trailing spaces of the string.

strip ()  function in python removes all the leading and trailing whitespaces of the string. strip() Function is the combination of lstrip() and rstrip() Function.

 

syntax of lstrip(),  rstrip() and strip() function in python:

1. str.lstrip([chars])
2. str.rstrip([chars])
3. str.strip([chars])

chars – The characters to be stripped from beginning or end of the string. when left blank it strips the whitespace

 

Example of lstrip() Function in python:

lstrip() function in python strips the leading whitespace

str1 = "      Salute to the mother earth";
str2 = "00000000Salute to the mother earth";
print str1.lstrip()
print str2.lstrip("0")
  • First lstrip() Function strips or removes the leading whitespaces from the string
  • Second lstrip() function strips or removes the leading Zeros from the string

So the output will be

Salute to the mother earth
Salute to the mother earth

 

Example of rstrip() Function in python:

rstrip() function in python strips the trailing whitespace

str1 = "Salute to the mother earth           ";
str2 = "Salute to the mother earth0000000";

print str1.rstrip()
print str2.rstrip("0")
  • First rstrip() Function strips or removes the trailing whitespaces from the string
  • Second rstrip() function strips or removes the trailing Zeros from the string

So the output will be

Salute to the mother earth
Salute to the mother earth

 

Example of strip() Function in python:

strip() function in python strips both leading and  trailing whitespace. strip() Function is the combination of lstrip() and rstrip() Function.

str1 = "      Salute to the mother earth           ";
str2 = "00000Salute to the mother earth0000000";

print str1.strip()
print str2.strip("0")
  • First strip() Function strips or removes the leading and trailing whitespaces from the string
  • Second strip() function strips or removes the leading and trailing Zeros from the string

So the output will be

Salute to the mother earth
Salute to the mother earth

 

previous lstrip(),rstrip() and strip() function in python                                                                                                           next lstrip(),rstrip() and strip() function in python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.