Text data from user input or files often contains unintended “whitespace” or “newline codes” at the beginning or end. If these are left remaining, they can cause errors when comparing strings or saving data to a database.
Python provides three string methods to easily remove these unnecessary characters: strip(), lstrip(), and rstrip().
In this article, I will explain the differences between each method and their specific use cases.
Table of Contents
- Remove whitespace from both ends:
strip() - Remove whitespace from the start only:
lstrip() - Remove whitespace from the end only:
rstrip() - Advanced: Removing specific characters
- Summary
1. Remove whitespace from both ends: strip()
The strip() method removes all whitespace characters from both the beginning (left) and the end (right) of a string.
Here, “whitespace characters” include not only standard spaces but also:
- Full-width spaces (depending on Python’s specification)
- Tabs (
\t) - Newlines (
\n,\r)
Specific Use Case: Cleaning User Input
This is effective when a user accidentally adds spaces before or after their input in a form.
# User input ID (contains spaces and newlines at both ends)
user_input_id = " user001 \n"
# Remove unnecessary characters from both ends using strip()
cleaned_id = user_input_id.strip()
print(f"Original: '{user_input_id}'")
print(f"Result : '{cleaned_id}'")
# Comparison works correctly
if cleaned_id == "user001":
print("ID matched.")
Execution Result:
Original: ' user001
'
Result : 'user001'
ID matched.
2. Remove whitespace from the start only: lstrip()
The lstrip() (Left Strip) method removes whitespace characters only from the left side (beginning) of the string. Whitespace at the end remains.
Specific Use Case: Removing Indentation
Use this when you want to remove indentation (spaces or tabs) from the beginning of a line.
# Source code line with indentation at the start
code_line = " print('Hello')"
# Remove whitespace from the left side
no_indent = code_line.lstrip()
print(f"Original: '{code_line}'")
print(f"After : '{no_indent}'")
Execution Result:
Original: ' print('Hello')'
After : 'print('Hello')'
3. Remove whitespace from the end only: rstrip()
The rstrip() (Right Strip) method removes whitespace characters only from the right side (end) of the string.
Specific Use Case: Removing Newline Codes
This is frequently used to remove the newline character (\n) attached to the end of a line when reading from a file.
# Line read from a file (has a newline at the end)
file_line = "data_record,123\n"
# Remove whitespace (including newline) from the right side
clean_line = file_line.rstrip()
print(f"Original: '{file_line}'")
print(f"After : '{clean_line}'")
Execution Result:
Original: 'data_record,123
'
After : 'data_record,123'
Advanced: Removing specific characters
By passing a string as an argument to these methods, you can remove specific characters from the ends instead of whitespace.
# Remove asterisks (*) and spaces from both ends
# Continues to remove characters as long as they match the argument
header_text = "** Chapter 1 **"
cleaned_header = header_text.strip("* ")
print(cleaned_header)
Execution Result:
Chapter 1
Summary
strip(): Removes whitespace and newlines from both ends (Most common).lstrip(): Removes from the left side (start) only.rstrip(): Removes from the right side (end) only (Often used for removing newlines).- Arguments: You can remove specific characters other than whitespace by specifying them as arguments.
