In Python, a consecutive sequence of the valid characters, always written within the single or double quotes, is referred to as a string. The string may have contained alphabets, digits, symbols or even whitespaces. Therefore, while doing the parsing of string content, we may get extra whitespaces or the characters that are not required anymore. And hence, and we have to remove them.
The Python stripping functions are very useful in this situation. These are very convenient too. In Python, we have three types of such stripping functions. One such function is the python lstrip() function. In this article, we are providing complete knowledge about the python lstrip string function as well as its applications in a different scenario.
- Python lstrip() function will return a new string after the removal of the leading characters from the input string. Leading characters will be based on the string passed as the argument.
- The Python lstrip() function will return a copy of the string after the removal of all the characters from the left end of the input string. Leading characters will be based on the string passed as the argument.
- lstrip() Syntax:
Syntax followed by the lstrip() function is:
The lstrip() function is accepting one or zero parameters. This is an optional parameter. Thus “chars” will represent one input string, which is specifying the set of leading characters to be removed from “chars”. It must be noted that, if the “chars” parameter is not provided, then the leftmost whitespaces of the string will be removed by default.
The valid whitespaces in Python are as given below:
- ‘ ‘ representing Space
- ‘\t’ representing Horizontal tab
- ‘\v’ representing Vertical tab
- ‘\n’ representing Newline
- ‘\r’ representing Carriage return
- ‘\f’ representing Feed
Return value from python lstrip():
The lstrip() function will return a new string with the removal of leading characters from the original input string. Also, all combinations of characters in the “chars” parameter will be stripped from the left of the string until the left character of the string mismatches.
Example 1: Working of lstrip()
# Python code using lstrip() function # Removal of all leading whitespaces mystring1= " Hello India " print("Before using function:", mystring1) newstring1=mystring1.lstrip() print("After using function:", newstring1) # Removal of all leading whitespaces mystring2= "Hello India" print("Before using function:", mystring2) newstring2=mystring2.lstrip("Hel") print("After using function:", newstring2) Output: Before using function: Hello India After using function: Hello India Before using function: Hello India After using function: o India
Example 2: How does lstrip() work?
To get propwer illustration of lstrip() function, we can specify one or more characters as a string which will be removed from the string in any sequence.
# Python program to illustrate lstrip() function.
txt1 = “123486491212321”
print(“Before function:”, txt1)
print(“After function:”, txt1.lstrip(“321”))
txt2 = “rraabbcrraabbd”
print(“Before function:”, txt2)
print(“After function:”, txt2.lstrip(“rraa”))
txt3 = “****$$$$&&&&”
print(“Before function:”, txt3)
print(“After function:”, txt3.lstrip(“**”))
Before function: 123486491212321
After function: 486491212321
Before function: rraabbcrraabbd
After function: bbcrraabbd
Before function: ****$$$$&&&&
After function: $$$$&&&&
In string txt1, we are giving some characters for removal. For example, string “321” will be removed from the left side of txt1. Then txt1.lstrip(“321”) removes all the leading characters ‘1’, ‘2’, and ‘3’ from the string irrespective of their order of occurrences.
Frequently Asked Questions:
Q.1: What is strip() function in Python?
Answer: Strip() function is used for string manipulation in Python. It will return the manipulated string after removing the spaces both on the left as well as on the right of the input string. Its syntax is string.strip().
>>> mystr = ‘ Hello World!’
Q.2. What is the output based difference between lstrip() and rstrip() functions in Python?
Answer: In Python language, three stripping functions are used. These are strip(), lstrip() and rstrip(). Function rstrip() is used to remove all trailing whitespaces or specific characters given as arguments to the function. On the other hand function, lstrip() is used to remove all leading whitespaces or specific characters given as arguments to the function
Q.3: What is the return value of the string method lstrip ()?
Answer: Function lstrip() will return another string which will be the copy of the original string but without any leading whitespace.