Skip to content
Related Articles

Related Articles

Python | Remove unwanted spaces from string

Improve Article
Save Article
  • Last Updated : 06 Aug, 2022
Improve Article
Save Article

Sometimes, while working with strings, we may have situations in which we might have more than 1 spaces between intermediate words in strings that are mostly unwanted. This type of situations can occur in web development and often needs rectification. Let’s discuss certain ways in which this task can be performed. Method #1 : Using re.sub() This problem can be performed using the regex in which we can restrict the separation between the words to be just a single space using the appropriate regex string. 

Python3




# Python3 code to demonstrate working of
# remove additional space from string
# Using re.sub()
import re
 
# initializing string
test_str = "GfG  is   good           website"
 
# printing original string
print("The original string is : " + test_str)
 
# using re.sub()
# remove additional space from string
res = re.sub(' +', ' ', test_str)
 
# printing result
print("The strings after extra space removal : " + str(res))

Output : 

The original string is : GfG  is   good           website
The strings after extra space removal : GfG is good website

  Method #2 : Using split() and join() This task can also be performed using the split and join function. This is performed in two steps. In first step, we convert the string into list of words and then join with a single space using the join function. 

Python3




# Python3 code to demonstrate working of
# remove additional space from string
# Using split() + join()
 
# initializing string
test_str = "GfG  is   good           website"
 
# printing original string
print("The original string is : " + test_str)
 
# using split() + join()
# remove additional space from string
res = " ".join(test_str.split())
 
# printing result
print("The strings after extra space removal : " + str(res))

Output : 

The original string is : GfG  is   good           website
The strings after extra space removal : GfG is good website

 Method #3 : Using split() with strip() and join() This task can also be performed using the split with strip and join function. This is performed in two steps. In first step, we convert the string into list of words and then join with a single space using the join function. 

Python3




# Python3 code to demonstrate working of
# remove additional space from string
# Using strip() and split() + join()
 
# initializing string
test_str = "GfG is good         website"
 
# printing original string
print("The original string is : " + test_str)
 
# using split() + join()
# remove additional space from string
res = " ".join(test_str.strip().split())
 
# printing result
print("The strings after extra space removal : " + str(res))

Output:

The original string is : GfG is good         website
The strings after extra space removal : GfG is good website

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!