How To Remove Multiple Spaces From A String In Python

Remove multiple spaces from a string in Python

To remove multiple spaces from a string in Python, we have tested effectively with two methods using Python regex sub-function and string split() and join(). Check out the details below to get more information.

Remove multiple spaces from a string in Python

Using Python regex sub-function

A simple way to remove multiple spaces from a string in Python is using Regular Expression. RegEx can be used to check if a string contains the specified search pattern.

And we can use the sub-function in Regular Expression to remove spaces from a string.

Syntax:

re.sub(reg, txt)

Parameters:

  • reg: Regular expression.
  • txt: a String use wants to replace. 

Return value: the return value replaces one or many matches with a string. 

Example:

In this example, we will use regular expression in python, sub() function in re to multiple convert spaces to only space for the message.

# Import regular expression
import re

message = "Welcome         to Learn        Share IT."
print("Before removing multi spaces")
print(message)

newMessage = re.sub("\s\s+" , " ", message) # \s\s+ regular expression
print("After removing multi spaces")
print(newMessage)

Output:

Before removing multi spaces
Welcome         to Learn        Share IT.
After removing multi spaces
Welcome to Learn Share IT.

Using string split() and join()

You can split the string by using the split() method and save the result into a list of words, then use the join() method to join the list into a string.

MethodsSyntaxParameters
split()
The split() method splits a string into a list. You can use any character to split the string
string.split(sep, max)sep: the separator to use when splitting the string.
max: The default value is -1, and it will show the max split to do.
join()
The join() method will join all items in a list or tuple into one string.
string.join(iterable)iterable: any iterable object where all the returned values are strings.

Example:

In this example, we use the split() method to split the message by white space and save the result into the list, then use the join() method with the message as ” ” to join the words into the string

message = "Welcome         to Learn        Share IT."
print("Before removing multi spaces")
print(message)

list = message.split()
print("Split the string into the list")
print(list)

newMessage = " ".join(list)
print("After removing multi spaces")
print(newMessage)

Output:

Before removing multi spaces
Welcome         to Learn        Share IT.
Split the string into the list
['Welcome', 'to', 'Learn', 'Share', 'IT.']
After removing multi spaces
Welcome to Learn Share IT.

Summary

In this tutorial, we have explained how to remove multiple spaces from a string using Python regex sub-function and string split() and join(). We always hope this tutorial is helpful to you. Leave your comment here if you have questions or comments about improving the article. Thanks for your read.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *