Last Updated On By Khizer Ali
We use the strip() method to remove unwanted whitespaces or characters/strings from both sides of the given string. What if we want to remove them from only one side? We can use lstrip() and rstrip() methods to serve this purpose. We use the lstrip() method to remove unwanted whitespaces or characters/strings from the given string’s left-side position. And the rstrip() method is used to remove unwanted whitespaces or characters/strings from the given string’s right side.
Table of Contents
string.lstrip([character])
string.rstrip([character])
string = "!Welcome to Code Leaks!"
striped_string = string.lstrip("!")
print(striped_string)
string = "!Welcome to Code Leaks!"
striped_string = string.rstrip()
print(striped_string)
In this article, we discussed the lstrip() and rstrip() methods in Python, which we use to remove unwanted whitespaces/characters/string from a given string either from the left or right. The lstrip() method removes it from the string’s left side, and the rstrip() method removes it from the right side of the string.