How to trim whitespace from a string in Python?

In this article, we will discuss how to trim whitespace from a string in Python.

Table Of Contents

Trim leading whitespaces from a string using lstrip()

In this scenario, we will see how to remove whitespaces from the left side of a string using the lstrip() method.

Syntax:

inp_str.lstrip()

Where inp_str is the input string. It returns a copy of the calling string object, with leading whitespace removed.

Advertisements

Example 1:

In this example, we will remove white spaces only on the left side of the string.

inp_str= "   Welcome to thisPointer   "

print("Actual String: ",inp_str)

# Remove whitespaces from the front of string
inp_str = inp_str.lstrip()

print(f'After removing whitespaces on left side: \'{inp_str}\'')

Output:

Actual String:     Welcome to thisPointer   
After removing whitespaces on left side: 'Welcome to thisPointer   '

Whitespaces from the left side are removed from the string.

Example 2:

In this example, we will remove white spaces only from the left side of the string.

inp_str= "   Hello   Welcome to thisPointer   "

print("Actual String: ",inp_str)

# Remove whitespaces from the front of string
inp_str = inp_str.lstrip()

print(f'After removing whitespaces on left side: \'{inp_str}\'')

Output:

Actual String:     Hello   Welcome to thisPointer   
After removing whitespaces on left side: 'Hello   Welcome to thisPointer   '

It removed the leading whitespaces only i.e. the Whitespaces on the left side. All other whitespaces remained as it is.

Trim trailing whitespaces from a string using rstrip()

In this scenario, we will see how to remove whitespaces from the right side of the string using rstrip() method.

Syntax:

inp_str.rstrip()

Where inp_str is the input string. It returns a copy of the calling string object, with trailing whitespace removed.

Example 1:

In this example, we will remove white spaces only from the right side of the string i.e. from the end of the string.

inp_str= "   Welcome to thisPointer   "

print("Actual String: ",inp_str)

# Remove whitespaces from the end of string
inp_str = inp_str.rstrip()

print(f'After removing whitespaces from the right side: \'{inp_str}\'')

Output:

Actual String:     Welcome to thisPointer   
After removing whitespaces from the right side: '   Welcome to thisPointer'

Whitespaces from the right side are removed from the string.

Example 2:

In this example, we will remove white spaces only on the right side of the string.

inp_str= "   Hello   Welcome to thisPointer   "

print("Actual String: ",inp_str)

# Remove whitespaces from the end of string
inp_str = inp_str.rstrip()

print(f'After removing whitespaces from the right side: \'{inp_str}\'')

Output:

Actual String:     Hello   Welcome to thisPointer   
After removing whitespaces on right side: '   Hello   Welcome to thisPointer'

Whitespaces from the right side are removed from the string.

Trim leading & trailing whitespaces from a string using strip()

In this scenario, we will see how to remove whitespaces from both the ends of a string using the strip() method.

Syntax:

inp_str.strip()

Where inp_str is the input string. It returns a copy of string after removing the leading and trailing whitespaces.

Example 1:

In this example, we will remove all white spaces from both the ends of a string.

inp_str= "   Welcome to thisPointer   "

print("Actual String: ",inp_str)

# Remove whitespaces from the front and end of string
inp_str = inp_str.strip()

print(f'After removing whitespaces from front and end: \'{inp_str}\'')

Output:

Actual String:     Welcome to thisPointer   
After removing whitespaces from front and end: 'Welcome to thisPointer'

Whitespaces from both the ends of string are removed.

Example 2:

In this example, we will remove all white spaces from front and end of the string.

inp_str= "   Hello   Welcome to thisPointer   "

print("Actual String: ",inp_str)

# Remove whitespaces from the front and end of string
inp_str = inp_str.strip()

print(f'After removing whitespaces from front and end: \'{inp_str}\'')

Output:

Actual String:     Hello   Welcome to thisPointer   
After removing whitespaces from front and end: 'Hello   Welcome to thisPointer'

Whitespaces from both the ends of a string are removed.

Trim leading & trailing whitespaces from a string using regular expression

In this scenario, we will see how to remove leading & trailing whitespaces from the string using the sub() method. It is available in the re module. This function accepts a regex pattern, a substring and a string as arguments. It looks for all the substrings in the given string that matches the given pattern. Then replaces all the matches with the given substring. We can use this to replace all leading & trailing whitespaces with empty string.

Syntax:

re.sub(r"^\s+|\s+$", "", inp_str)

Parameters:
1. the first parameter is the pattern to remove white spaces
2. the second parameter specifies the empty string that replaces the white spaces
3. input_str is the input string.

It will replace all leading & trailing whitespaces with empty string.

Example 1:

In this example, we will remove all white spaces from the string.

import re

inp_str= "   Welcome to thisPointer   "

print("Actual String: ",inp_str)

# Remove whitespaces from the front and end of string
inp_str = re.sub(r"^\s+|\s+$", "", inp_str)

print(f'After removing whitespaces from front and end: \'{inp_str}\'')

Output:

Actual String:     Welcome to thisPointer   
After removing whitespaces from front and end: 'Welcome to thisPointer'

Whitespaces are removed from front and end of the string.

Example 2:

In this example, we will remove all white spaces from the string.

import re

inp_str= "   Hello   Welcome to thisPointer   "

print("Actual String: ",inp_str)

# Remove whitespaces from the front and end of string
inp_str = re.sub(r"^\s+|\s+$", "", inp_str)

print(f'After removing whitespaces from front and end: \'{inp_str}\'')

Output:

Actual String:     Hello   Welcome to thisPointer   
After removing whitespaces from front and end: 'Hello   Welcome to thisPointer'

Whitespaces are removed from front and end of the string.

Summary

We explored four ways to remove white spaces using lstrip(),rstrip(), strip() and re.sub() methods. If we want to remove white spaces on the left side, you can use lstrip(). If we want to remove white spaces on the right side, you can use rstrip(). Happy Learning

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.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top