Remove multiple spaces from a string in Python

In this Python tutorial, we will learn how to remove multiple spaces from a string.

Table Of Contents

Remove multiple spaces from a string using re.sub()

The re is a Regex module in Python. It has a method sub(), and it is used to replace substrings in strings, that matches a given regex pattern. We can use the same to remove multiple spaces from a string. For that we need to use a regex pattern that matches one or more whitespaces. Then look for the substrings that matches the given regex pattern and replace them with single whitespace. Let’s see the syntax of sub() with the parameters that we need to pass in it to remove multiple spaces.

Syntax:

re.sub("\s+", " ", strValue)

Parameters:

Advertisements
  1. the First parameter is a regex pattern that will look for one or more whitespaces.
  2. the second parameter represents a single whitespace
  3. strValue is the actual input string.

It will return a string in which all multiple spaces are replaced by a single space.

Example 1:

In this example, we will consider the string with multiple spaces and remove these multiple spaces using re.sub() method.

import re

# Consider a string with multiple spaces
strValue = "Welcome     to     thisPointer,learn                Python language"

print("Original String: \"", strValue, "\"")

# Remove multiple spaces from string
strValue = re.sub("\s+", " ", strValue)

print("String after removing multiple spaces: \"", strValue, "\"")

Output:

Original String: " Welcome     to     thisPointer,learn                Python language "
String after removing multiple spaces: " Welcome to thisPointer,learn Python language "

You can see that multiple spaces are removed between

  1. welcome and to
  2. to and thisPointer
  3. learn and Python

Example 2:

In this example, we will consider the string with a single space and remove these multiple spaces using re.sub() method.

import re

# Consider a string with multiple spaces
strValue = "Hello           Python"

print("Original String: \"", strValue, "\"")

# Remove multiple spaces from string
strValue = re.sub("\s+", " ", strValue)

print("String after removing multiple spaces: \"", strValue, "\"")

Output:

Original String: " Hello           Python "
String after removing multiple spaces: " Hello Python "

You can see that multiple spaces are removed between Hello and Python.

Remove multiple spaces from a string using join() with split()

we can split a string using the split() function of string class. By default it will use a whitespace character as delimeter to split the string into multiple substrings. Then join all of theese substrings with a single space as separator using the join() method of string class. It will remove the multiple spaces from the string.

Example:

In this example, we have a string with multiple spaces and we will remove these multiple spaces from it using the join() method with split().

strValue = "Welcome     to     thisPointer,learn                Python language"

print("Original String: \"", strValue, "\"")

# Remove multiple spaces from string
strValue = " ".join(strValue.split())

print("String after removing multiple spaces: \"", strValue, "\"")

Output:

Original String: " Welcome     to     thisPointer,learn                Python language "
String after removing multiple spaces: " Welcome to thisPointer,learn Python language "

You can see that multiple spaces are removed between

  1. welcome and to
  2. to and thisPointer
  3. learn and Python

Summary

In this article, we saw how to remove multiple spaces from a string in Python using different techniques. The best method is using split() with join() because no extra module is required to use this method. 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.

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