str.lstrip()

Added in v3.x · Updated March 13, 2026 · String Methods
stdlib string character-stripping

The .lstrip() method returns a copy of the string with leading characters removed. By default, it removes whitespace (spaces, newlines, tabs). You can pass a string of characters to remove specific characters from the beginning of the string.

This method is useful for cleaning up user input, parsing formatted text, and removing unwanted prefixes.

Signature

str.lstrip([chars])

Parameters

ParameterTypeDefaultDescription
charsstrNoneA string specifying characters to remove. If None, removes all leading whitespace.

Return Value

Returns a new string with leading characters removed. The original string remains unchanged.

Return type: str

Basic Examples

Remove leading whitespace

text = "   hello world"
result = text.lstrip()
print(result)  # Output: hello world

Remove specific characters

text = "xxhelloxx"
result = text.lstrip("x")
print(result)  # Output: helloxx

Only removes from the beginning

text = "   hello world   "
result = text.lstrip()
print(result)  # Output: hello world   (trailing spaces preserved)

Practical Use Cases

Cleaning user input

username = "   john_doe  "
cleaned = username.lstrip()  # Only remove leading; rstrip handles trailing
print(repr(cleaned))  # Output: 'john_doe  '

Removing text prefixes

# Remove leading markers from a list of items
items = ["***item1", "***item2", "***item3"]
cleaned = [item.lstrip("*") for item in items]
print(cleaned)  # Output: ['item1', 'item2', 'item3']

Parsing multi-line text

# Remove leading indentation from multiline strings
code = """
    def hello():
        print("world")
"""
# Remove leading whitespace from each line
result = code.lstrip("\n").split("\n")
print(result[1])  # Output: '    def hello():'

Character Set Behavior

The chars parameter works as a set of characters, not a substring. Python removes any combination of these characters from the start until it hits a character not in the set.

# 'abc' is treated as {'a', 'b', 'c'}
text = "aaabbbccc"
result = text.lstrip("abc")
print(result)  # Output: '' (all characters removed)

# Stops at first character not in the set
text = "aaabbbxyz"
result = text.lstrip("abc")
print(result)  # Output: 'xyz'

This differs from substring matching. If you need to remove a specific prefix string, use slicing:

text = "prefix_value"
if text.startswith("prefix_"):
    result = text[len("prefix_"):]
else:
    result = text
print(result)  # Output: value

See Also