This is a tutorial of the Python String lstrip() method. Learn to get the copy of a given string with specified leading characters removed from it.
This method returns a new copy of the given string in which the characters specified in the argument are removed from the leading edge of the string. (From Left-hand side)
Note. By default this method removes the leading whitespaces, in case, no argument is supplied to this method. The characters to be removed has to be provided as a string containing the set of characters to be removed.
Given below is the syntax for the Python String
leftStrippedString = givenString.lstrip(chars)
This method only takes a single argument and that is the
chars. It is basically a string type argument that can contain a number of characters. These are the characters whose combinations you want to be removed from the leading edge of the given string on which this method is applied.
In case, this argument is not passed to this method, all of the leading whitespace characters will be removed by this method.
str.lstrip() Return Value
It returns the new copy of the string in which the specified leading characters will be stripped off from the given string on which this method is applied.
If you provide more than one character as the string type argument of this method, then all of the possible combinations that are possible to be created using the string characters, if existing in the leading edge of the given string will be removed or stripped until it founds the first mismatch.
Example: Basic usage of the Python String lstrip() method
Here in the following example, we’ve applied the method
lstrip() on four different string to check what all characters are being removed in the returned copy of the string from the leading edge with different arguments.
string1 = " hello, how are you?" #Using lstrip() without chars argument #Leading whitespaces will be removed print(string1.lstrip()) string2 = "yo hello world!" #Char Argument Supplied #No Space Character Mentioned print(string2.lstrip('yohel')) #Space Character Mentioned print(string2.lstrip('yohel ')) string3 = "C, C++, Java, Python" print(string3.lstrip('c, +')) #Case Matters print(string3.lstrip('C, +')) string4 = "121213121212" print(string4.lstrip("12"))
hello, how are you? hello world! world! C, C++, Java, Python Java, Python 3121212
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!