In this tutorial, we will learn about some methods using which we can remove leading whitespaces from a string in Python Programming language.
Table Of Contents
Suppose we have a string with leading whitespaces i.e.
This is a sample text.
Now we want to remove leading whitespaces from this string. Basically we want to remove all spaces/tabs etc from the left of string. Final string should be like,
This is a sample text.
There are different ways to achieve this. Let’s discuss them one by one.
Method 1: Using lstrip()
First method that we will be using to remove leading whitespaces from a string is the str.lstrip() method.
The lstrip() method of string class can remove any given character or whitespace from the left side of the string. This methods recieves only one parameter, which is the character that needs to be removed from the left of string.Default value of this character is the whitespace. It returns a copy of the string after removing all occurrences of that character from the left of string.
So, to remove all leading whitespaces from the string, just call this method with default arguments. Let’s see an example,
# Initialized a string with leading whitespaces. example_string = ' Removed whitespace using lstrip() method.' print(example_string) # Removing leading whitespaces from string example_string = example_string.lstrip() print(example_string)
Removed whitespace using lstrip() method. Removed whitespace using lstrip() method.
In the above example, we have successfully removed all the leading whitespace from the string.
Method 2: Using sub()
In Python, the regex module provides a function for the string substitution i.e. sub(). This method takes three arguments i.e.
* A regex pattern
* A replacement string
* A string in which replacement needs to be done.
It looks for all substrings in the given string that matches the given pattern and replaces them with the given replacement string.
To remove leading whitespaces from a string use a regex pattern “^\s+” and replace all matched substrings with an empty string. Let’s see the complete example.
import re # Initialized a string with leading whitespaces. example_string = ' Removed whitespace using sub() method.' print(example_string) # removing leading white space using sub() function example_string = re.sub(r"^\s+", "", example_string) print(example_string)
Removed whitespace using sub() method. Removed whitespace using sub() method.
In the above example, we have passed three parameters in the sub() function,
- First is the regex pattern that matches all leading whitespaces
- Second is replacement string i.e. an empty string.
- Third is the string from which we want to remove leading whitespace.
It successfully removed all the leading whitespaces from the string.
In this python tutorial we learned about two different methods by using these methods we can remove leading whitespaces from a string. You can always use any of the above methods. But the method with best readability and easy to understand approach with minimal syntax is the str.lstrip() method. It simply removes all the leading whitespace from the beginning.
Also we have used Python 3.9.12 for writing example codes. Type python –version in your terminal to check your python version. Always try to read, write and run example codes on your machine to understand properly. Thanks.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.