Methods and Functions

Python String lstrip()

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

python lstrip

Definition:

  • 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:

string.lstrip([chars])

lstrip() Parameters:

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:

  1. ‘ ‘ representing Space
  2. ‘\t’ representing Horizontal tab
  3. ‘\v’ representing Vertical tab
  4. ‘\n’ representing Newline
  5. ‘\r’ representing Carriage return
  6. ‘\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(“**”))

Output:

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().

Example:

                    

>>> mystr = ‘ Hello World!’

>>> mystr1.strip()

‘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

Example:

                    

>>> txt=”####asdf####”

>>> txt.lstrip(“#”)

‘asdf####’

>>> txt.rstrip(“#”)

‘####asdf’

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.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.