Python | Remove unwanted spaces from string
Last Updated :
23 Apr, 2023
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 situation can occur in web development and often needs rectification. Let’s discuss certain ways in which this task can be performed.
Examples:
Example 1:
Input: GfG is good website
Output: GfG is good website
Explanation: Unwanted spaces have been removed from the string.
Example 2:
Input: GfG is good website
Output: GfG is good website
Explanation: Unwanted spaces have been removed from the string.
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
import re
test_str = "GfG is good website"
print ( "The original string is : " + test_str)
res = re.sub( ' +' , ' ' , test_str)
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
Time Complexity: O(n)
Space Complexity: O(n)
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
test_str = "GfG is good website"
print ( "The original string is : " + test_str)
res = " " .join(test_str.split())
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
Time Complexity: O(n)
Space Complexity: O(n)
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
test_str = "GfG is good website"
print ( "The original string is : " + test_str)
res = " " .join(test_str.strip().split())
print ( "The strings after extra space removal : " + str (res))
|
Method 4: Using a loop to iterate over each character of the string
Initialize an empty string to store the result
Use a loop to iterate over each character of the input string
If the current character is a space and the previous character was also a space, skip it
Otherwise, append the current character to the result string
Python3
test_str = "GfG is good website"
print ( "The original string is : " + test_str)
res = ""
for i in range ( len (test_str)):
if i = = 0 or (test_str[i] ! = " " or test_str[i - 1 ] ! = " " ):
res + = test_str[i]
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
Time complexity: O(n)
Auxiliary space: O(n) (to store the result string)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...