Python String lstrip() Method – Remove Leading Characters

Python String lstrip() Method

The lstrip() method in Python removes all leading characters (default is whitespace) from the beginning (left side) of the string.

Syntax

string.lstrip([chars])

Parameters:

  • chars (optional): A string specifying the set of characters to remove. If not provided, it removes leading whitespace.

Returns:

  • A new string with the specified leading characters removed.

Example 1: Remove Leading Whitespace

text = "   hello world"
print(text.lstrip())
hello world

Example 2: Remove Specific Leading Characters

text = "www.example.com"
print(text.lstrip("w."))
example.com

Why? Because lstrip("w.") removes all characters 'w' and '.' from the beginning until it hits a character not in the set.

Use Cases

  • Cleaning user input by trimming extra spaces from the start
  • Stripping formatting characters like 0s, #s, or ws from the beginning of strings
  • Removing URL prefixes or padding characters

Common Mistakes

  • It does not remove characters in the middle or end – only from the start
  • lstrip("abc") removes any combination of 'a', 'b', or 'c' from the start – not the full string "abc"

Interview Tip

String cleaning is a frequent task in data preprocessing. Know how lstrip(), rstrip(), and strip() differ.

Summary

  • string.lstrip() removes unwanted characters from the beginning of the string
  • By default, it removes leading whitespace
  • Does not affect the original string (returns a new one)

Practice Problem

Write a program to clean user input that may have leading dashes or spaces:

user_input = "--   Hello Python"
cleaned = user_input.lstrip("- ").strip()
print("Cleaned:", cleaned)

Expected Output:

Cleaned: Hello Python