Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to remove all leading whitespace in string in Python?
Leading whitespace refers to any spaces, tabs, or other blank characters that appear at the start of a string. These characters come before the first visible text and can affect how the string is displayed or processed.
Python provides several methods to remove leading whitespace from strings. The most common and recommended approach is using the lstrip() function.
Using the lstrip() Function
The lstrip() function removes all whitespace characters from the beginning (left side) of a string. It does not affect any whitespace at the end or in the middle of the string.
Syntax
string.lstrip([chars])
Parameters:
- chars (optional) − Specific characters to remove from the left. If not provided, removes all whitespace characters (spaces, tabs, newlines).
Example: Basic Usage
Here's how to remove leading whitespace using lstrip() ?
text = " Hello Python"
cleaned_text = text.lstrip()
print("Original:", repr(text))
print("Cleaned:", repr(cleaned_text))
The output of the above code is ?
Original: ' Hello Python' Cleaned: 'Hello Python'
Example: Different Types of Whitespace
The lstrip() function removes various types of whitespace characters ?
text_with_tabs = "\t\t Hello World"
text_with_newlines = "\n\n Python Programming"
print("With tabs:", repr(text_with_tabs.lstrip()))
print("With newlines:", repr(text_with_newlines.lstrip()))
With tabs: 'Hello World' With newlines: 'Python Programming'
Using Regular Expressions
For more control over which characters to remove, you can use regular expressions with the re.sub() function ?
import re
text = " \t\n Hello World"
cleaned = re.sub(r'^\s+', '', text)
print("Original:", repr(text))
print("Cleaned:", repr(cleaned))
Original: ' \t\n Hello World' Cleaned: 'Hello World'
Comparison of Related Functions
Python provides three related string methods for whitespace removal ?
| Method | Removes From | Use Case |
|---|---|---|
lstrip() |
Left (beginning) | Remove leading whitespace only |
rstrip() |
Right (end) | Remove trailing whitespace only |
strip() |
Both ends | Remove leading and trailing whitespace |
Example: Comparing All Three Methods
text = " Hello Python "
print("Original:", repr(text))
print("lstrip():", repr(text.lstrip()))
print("rstrip():", repr(text.rstrip()))
print("strip():", repr(text.strip()))
Original: ' Hello Python ' lstrip(): 'Hello Python ' rstrip(): ' Hello Python' strip(): 'Hello Python'
Removing Specific Characters
You can specify which characters to remove from the beginning of a string ?
text = "...___Hello World"
cleaned = text.lstrip("._")
print("Original:", text)
print("Cleaned:", cleaned)
Original: ...___Hello World Cleaned: Hello World
Conclusion
Use lstrip() to remove leading whitespace from strings in Python. It's efficient, readable, and handles all types of whitespace characters. For specific character removal, pass the characters as an argument to lstrip().
