Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 05

lstrip(), rstrip(), and strip()

Learn how to remove spaces and unwanted characters from the left, right, or both ends of a string using visual memory diagrams.

Beginner15 min

What you'll learn

Topic Focus
lstrip() Remove from the left
rstrip() Remove from the right
strip() Remove both sides
Space cleanup Use split() + join()

Visual roadmap

All three methods remove characters from the edges, not the middle.

lstrip left, strip both, rstrip right

Easy memory: L = Left · R = Right · Strip = Both

1. lstrip() → Left strip

Removes whitespace (or characters) from the beginning of the string.

lstrip before and after — spaces removed from left
python
s = "    Python"

print(repr(s))
# '    Python'

s = s.lstrip()

print(repr(s))
# 'Python'

Character count:

Before After
Length 10 characters 6 characters

2. rstrip() → Right strip

Removes whitespace from the end of the string.

rstrip before and after — spaces removed from right
python
s = "Python   "

s = s.rstrip()

print(repr(s))
# 'Python'

Memory: R = Right = End

3. strip() → Both sides

Removes characters from both the beginning and end.

strip before and after — both sides trimmed
python
s = "   Python    "

s = s.strip()

print(repr(s))
# 'Python'

Memory formula:

Left + Right = strip()

lstrip() vs rstrip() vs strip()

Method Removes Visual
lstrip() Left only [spaces]Python
rstrip() Right only Python[spaces]
strip() Both sides [spaces]Python[spaces]

4. Remove custom characters

strip() is not limited to spaces. You can remove any character from the edges.

+++PYTHON+++ becomes PYTHON — edge + only
python
s = "++++++++PYTHON+++++++"

print(s.strip("+"))
# PYTHON

Another example:

python
s = "###Hello###"

print(s.strip("#"))
# Hello

Important: It removes characters only from the edges, not the middle.

5. Cleaning messy spaces

strip() removes outer spaces, but middle spaces remain.

Original → strip() → split()+join() space cleanup flow
python
s = "Nirkar   Behra    "

s = s.strip()

print(repr(s))
# 'Nirkar   Behra'

clean = " ".join(s.split())

print(clean)
# Nirkar Behra

How it works:

String → split() → join() flow

Playground example

python
s = "   Python    "

print(repr(s), len(s))

print(repr(s.strip()), len(s.strip()))

name = "Nirkar   Behra    "

print(" ".join(name.split()))

Expected output:

text
'   Python    ' 13
'Python' 6
Nirkar Behra

Use the lesson playground at the bottom of the page to run this code live.

Quick knowledge check

  1. Which method removes spaces only from the right?
    Answer: rstrip()

  2. Does strip() remove spaces in the middle?
    Answer: No. It only removes leading and trailing characters.

  3. How do you convert "a b c" into "a b c"?
    Answer:

python
" ".join(s.split())

30-second revision sheet

Strip family hub — lstrip, rstrip, strip, split+join
Method Easy memory
lstrip() Remove from left
rstrip() Remove from right
strip() Remove both sides
split() + join() Clean middle spaces

Interview shortcut: Left → lstrip() · Right → rstrip() · Both → strip() · Middle spaces → split() + join()

Next: Split and Join.

Loading playground…