Python lstrip() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

lstrip() removes characters from the left end (the start) of a string. With no argument it removes leading whitespace. The right end and the middle are left alone, so " hi ".lstrip() is "hi ".

Pass a string of characters to remove them instead. As with strip(), the argument is a set of characters, and the method keeps removing for as long as the text starts with one of them. The result is a new string.

text.lstrip() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Call it with no argument to trim leading whitespace, or pass the characters to remove.

python
clean = text.lstrip()
clean = text.lstrip(chars)
CallWhat it does
" hi ".lstrip()Returns 'hi '. Only the spaces on the left are removed.
"007".lstrip("0")Returns '7'. Both leading zeros are removed.
"xxhixx".lstrip("x")Returns 'hixx'. The trailing xx is not touched.

Use rstrip() for the right end and strip() for both ends. To remove an exact prefix use removeprefix().

Library declarations
python
str.lstrip([chars]) -> str
# returns: a new string; the original is unchanged
# chars: a string of characters to remove; default is whitespace

Parameters

ParameterDescription
charsOptional. A string whose characters are removed from the left end. The order does not matter. If it is omitted or None, whitespace is removed.

Return Value

Return ValueDescription
strA new string with the matching characters removed from the start. The original string is not changed.

Python Examples

Example 1 — Remove Leading Spaces Only

Compare the text before and after, and look at both ends.

python
text = "   hello world   "
left = text.lstrip()

print(repr(left))
print(len(text), len(left))
print(left.endswith(" "))

How It Works

  1. Only the start changes. The three leading spaces are removed, and the three trailing spaces stay.
  2. Three characters shorter. The length drops from 17 to 14.
  3. The right end is untouched. The new string still ends with a space, so endswith(" ") is True.

Example 2 — Clean Up Padded Numbers

Strip the zeros from padded codes, and keep a zero when nothing is left.

python
codes = ["0007", "0420", "0000"]
plain = [code.lstrip("0") for code in codes]
safe = [code.lstrip("0") or "0" for code in codes]

print(plain)
print(safe)

How It Works

  1. Leading zeros go. "0007" becomes "7" and "0420" becomes "420".
  2. An all-zero code vanishes. Every character is removed, so "0000" becomes an empty string.
  3. Keep one zero. An empty string is false, so or "0" replaces it with the text "0".

Example 3 — Count the Indentation of Each Line

The difference between a line and its left-stripped copy is the indentation.

python
lines = ["total = 0", "    if x:", "        total += x"]

for line in lines:
    depth = len(line) - len(line.lstrip())
    print(depth, line.lstrip())

How It Works

  1. Strip a copy. line.lstrip() is the line without its leading spaces.
  2. Subtract the lengths. The number of removed characters is the indentation depth.
  3. Three depths. The lines have 0, 4, and 8 leading spaces, and the code prints each depth beside the clean text.

Frequently Asked Questions

It returns a copy of the string with leading characters removed from the left end. By default it removes whitespace.
lstrip() trims only the left end, while strip() trims both ends.
Use text.lstrip("0"). If the text can be all zeros, use text.lstrip("0") or "0" so that you do not end up with an empty string.
Not exactly. The argument is a set of characters, so it removes any leading a, b, or c characters. Use removeprefix() for an exact prefix.
No. Strings are immutable, so lstrip() returns a new string. Assign the result to keep it.

Next: maketrans()

Build a translation table for translate() from characters to replace or delete.

maketrans() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful