Python Reference Library →

Python String lstrip() Method – Remove Leading Characters

Let’s look at string lstrip() method – remove leading characters

  1. Notice

    lstrip() removes leading whitespace by default. Its chars argument specifies a set of removable characters, not a prefix.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'ababa tea'
result = text.lstrip("ab ")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'ababa tea'Object 2 · str'tea'
This step

lstrip() removes leading whitespace by default. Its chars argument specifies a set of removable characters, not a prefix.

Picture string lstrip() method – remove leading characters

lstrip() removes leading whitespace by default. Its chars argument specifies a set of removable characters, not a prefix.

Image brief

Alt text
A diagram for Python String lstrip() Method – Remove Leading Characters. Instructions: text = 'ababa tea' result = text.lstrip("ab ") print(result). Displayed result: tea. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'ababa tea'; object 2: 'tea'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String lstrip() Method – Remove Leading Characters". Teaching focus: lstrip() removes leading whitespace by default. Its chars argument specifies a set of removable characters, not a prefix. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'ababa tea' result = text.lstrip("ab ") print(result) | v DISPLAYED RESULT tea FINAL BINDINGS text --> object 1 (str) result --> object 2 (str) object 1: 'ababa tea' object 2: 'tea' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String lstrip() Method – Remove Leading Characters. Instructions: text = 'ababa tea' result = text.lstrip("ab ") print(result). Displayed result: tea. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'ababa tea'; object 2: 'tea'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'ababa tea'
result = text.lstrip("ab ")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'ababa tea'Object 2 · str'tea'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
tea

What do you expect this program to print?

What does this example display?

text = 'ababa tea'
result = text.lstrip("ab ")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

tea

Follow the printed values in order. The output is: tea

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

lstrip() removes leading whitespace by default. Its chars argument specifies a set of removable characters, not a prefix.

lstrip() removes leading whitespace by default. Its chars argument specifies a set of removable characters, not a prefix.

More examples and reference

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

What to remember

Connect what you noticed in the example with the idea below.

Remember

lstrip() removes leading whitespace by default. Its chars argument specifies a set of removable characters, not a prefix.

Watch out for this idea

lstrip("ab") removes exactly one literal ab prefix.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →