Python – lstrip() Function

1. What is the lstrip() Function in Python? (Definition)

The lstrip() function is a built-in Python string method used to remove characters from the left side of a string. By default, it removes leading whitespace—such as spaces, tabs, or newline characters—but you can also specify a custom set of characters to strip.
It’s especially helpful when cleaning raw input, trimming formatting issues, or preparing text data for further processing. The original string stays unchanged because Python strings are immutable, and lstrip() always returns a new string.

2. Syntax of the lstrip() Method

Below is the synax of lstrip() Method.


str.lstrip([chars])

This syntax is simple and beginner-friendly, making it easy to use while processing string data.

3. Parameter Description

Parameter Type Description
chars string (optional) A string containing characters to remove from the start of the string. If omitted, Python removes leading whitespace (space, tab, newline).

If chars is provided, Python removes any character present in that set, not a sequence—all characters are treated individually.

4. Return Value

  • Returns a new string with leading characters removed.
  • If the starting characters do not match the ones in chars, the string is returned unchanged.
  • The original string is never modified due to string immutability in Python.

5. Examples of Using the lstrip() Function in Python Function

Example 1: Removing leading spaces (default behavior)


text = " Hello World! " print(text.lstrip()) # Output: "Hello World! "
Explanation:

By default, lstrip() removes whitespace on the left side of the string. The trailing spaces at the end stay untouched.

Example 2: Removing specific characters


text = "///path/to/file" print(text.lstrip("/")) # Output: "path/to/file"
Explanation:

All leading / characters are removed until Python encounters a different character.

Example 3: Removing multiple characters from the beginning


text = "abcabcHello" print(text.lstrip("abc")) # Output: "Hello"
Explanation:

Each leading character that matches ‘a’, ‘b’, or ‘c’ is stripped—regardless of order—until the first non-matching character is found.

Example 4: When the specified characters do not appear at the start


text = "Hello World" print(text.lstrip("H")) # Output: "ello World" print(text.lstrip("X")) # Output: "Hello World"
Explanation:

The first call removes the leading “H”. In the second call, since “X” is not at the start, the string remains unchanged.

Example 6: Using lstrip() on an empty string


empty = "" print(empty.lstrip()) # Output: ""
Explanation:

An empty string stays unchanged. Python handles it gracefully without errors.

Example 7: No matching characters to remove


text = "Hello" print(text.lstrip("xyz")) # Output: "Hello"
Explanation:

None of the characters x, y, or z appear at the start, so the original string is returned as-is.

Example 8: Removing punctuation from the start


text = "...Hello..." print(text.lstrip(".")) # Output: "Hello..."
Explanation:

All leading periods are removed, while the trailing ones stay in place.

Explanation:

Strings in Python are immutable, meaning they cannot be changed. .lower() returns a new lowercase string while the original stays the same.

6. Summary Table of Examples of lstrip() Method

Scenario Code Example Result
Remove leading spaces " Hello".lstrip() "Hello"
Remove specified char / "///path".lstrip("/") "path"
Remove multiple chars abc "abcabcHello".lstrip("abc") "Hello"
Remove char not present "Hello".lstrip("X") "Hello"
Remove whitespace incl. tabs "\n\t Hello".lstrip() "Hello"
Empty string input "".lstrip() ""
No matching leading char "Hello".lstrip("xyz") "Hello"
Remove leading punctuation "...Hello".lstrip(".") "Hello"

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top