Python lstrip() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
202784
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
202784
4.90
Start Learning
Topics Covered

Overview

The Python String lstrip() function produces a replica of the string without the initial few characters, depending on the string argument passed. It strips leading spaces when no argument is provided.

Syntax of Python lstrip() Function

'lstrip()' removes characters from the left side of the input string based on the string argument it receives, which specifies the characters to be removed.

The syntax of lstrip() is:

Parameters of Python lstrip() Function

The chars parameter, a string, specifies the characters that will be dropped from the clone of the string by the lstrip() function.

The chars parameter takes whitespace characters as default if you don't supply any argument or use None. The initial whitespace characters from the duplicate of the string will be eliminated in this situation by the lstrip() function.

Return Value of Python String lstrip() Function

Python lstrip() function returns a string with the initial characters removed.

Errors and Exceptions of Python lstrip() Function

When we attempt to use the lstrip() function on a data type other than a Python String, an AttributeError occurs.

Output:

Uses of Python lstrip() Function

lstrip() function is used to just remove leading whitespace and characters. When the user wish to purge the string's beginning simply of whitespace and characters, this is useful.

Examples of Python lstrip() Function

Use lstrip() function to just trim initial whitespace and characters.

  • When no parameter is passed in the lstrip() function, it removes the leading whitespaces by default.

Output:

  • Removing specific characters from the beginning of the string.

"Sam" being cut from a string offers as an example of this. To demonstrate how lstrip() function works,  "Sam" characters in this example is used at both the beginning and the end of the string.

Output:

  • Removing multiple characters using lstrip() function

Output:

In the above example, the lstrip() function removes the given set of characters from the left of the input string.

How Does the Python lstrip() Function Work?

  • The function looks for the given sequence or characters, at the start of the string and removes characters until it is halted by a distinct character that varies from the character/pattern passed.
  • When no arguments are supplied as a part of the function,  the spaces that lead the character string will be eliminated and returned by the lstrip function.
  • No modifications will be made to the string if no matches are identified.

FAQs

Q: Define "strip()" function in Python?

A: Python's strip() function is used to manipulate strings. It uses the syntax string.strip(). After deleting the spaces from the input string's left and right sides, it will return the altered string.

For example:

Output:

Q: Difference between "lstrip()" and "rstrip()"?

A: lstrip(): In the lstrip() function, "l" stands for left, as it eliminates the leading whitespace on the left of the string.

rstrip(): In the rstrip() function, "r" stands for right, as it eliminates the leading whitespace on the right of the string.

Conclusion

  • The lstrip() function of the Python string class is implemented to obtain a duplicate of a string without the beginning characters.
  • The lstrip() method in Python returns a string with the initial characters stripped.
  • The given characters will be stripped from the shared string when the character matches the given string.
  • The lstrip function stops removing leading characters from the left of the string when the character string specified there does not match the string that is to be stripped.

Explore Scaler Topics Python Tutorial and enhance your Python skills with Reading Tracks and Challenges.