>>> import re
>>> re.sub(' +', ' ', 'The     quick brown    fox')
'The quick brown fox'
Answer from Josh Lee on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-unwanted-spaces-from-string
Python | Remove unwanted spaces from string - GeeksforGeeks
April 23, 2023 - 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 code to demonstrate working of # remove additional space from string # Using strip() and split() + join() ...
Discussions

methods to remove spaces from text
You are missing a regex. And the best one is number 1. Because of readability. Plus numbers 2, 3 and 4 don't work. You are putting the space back. More on reddit.com
🌐 r/learnpython
16
6
December 20, 2023
How to remove extra space
Why there is extra spaces (marked yellow) before dot and how to get rid of them? x = float(input(“Give a decimal number: “)) y = int(input(“Give an integer number: “)) result = x**y print(x,“to the power of”,y,“is approximately”,(round(result)),”.”) print(“To be exact ... More on discuss.python.org
🌐 discuss.python.org
2
0
September 19, 2022
How to Remove Spaces From a String in Python (Complete 2026 Guide)
Q) How do I remove multiple consecutive spaces in Python? A) To collapse multiple spaces into a single space: ... This keeps word separation intact while removing extra spacing. More on accuweb.cloud
🌐 accuweb.cloud
1
December 1, 2023
How to remove whitespaces from a string in Python?
Hi, I want to know How to remove whitespaces from a string in Python More on discuss.python.org
🌐 discuss.python.org
2
0
March 8, 2021
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-spaces-from-string
Effective Ways to Remove Spaces from Strings in Python | DigitalOcean
July 11, 2025 - Master Python string manipulation by learning how to remove whitespace and manage spaces with techniques like strip(), replace(), and regex for cleaner data.
🌐
Reddit
reddit.com › r/learnpython › methods to remove spaces from text
r/learnpython on Reddit: methods to remove spaces from text
December 20, 2023 -

Hi everyone, I recently took the Fullstack Python Developer course and am now taking my first steps on this path.
Can you tell me what other methods of removing spaces from text you know and in which case, each of them is better to use?
So far I know 4 of them:

  1. replace method -> text_wo_spaces = text.replace(" ","")

  2. join and split methods -> text_wo_spaces = ' '.join(text.split())

  3. list generator + join -> text_wo_spaces = ' '.join([char for char in text if char != ' '])

  4. filter + join methods -> text_wo_spaces = ' '.join(filter(lambda char : != ' ', text))

🌐
Python.org
discuss.python.org › python help
How to remove extra space - Python Help - Discussions on Python.org
September 19, 2022 - Why there is extra spaces (marked ... number: “)) result = x**y print(x,“to the power of”,y,“is approximately”,(round(result)),”.”) print(“To be exact it is”,(round(result, 2)),”.”)...
🌐
Python Morsels
pythonmorsels.com › remove-spaces
How to remove spaces in Python - Python Morsels
January 25, 2022 - If you're trying to remove all sorts of whitespace characters (space, tab, newline, etc.) you could use the split and join string methods: If we call split on this version string, Python will split on all consecutive whitespace characters:
🌐
Mimo
mimo.org › tutorials › python › how-to-remove-spaces-from-a-string-in-python
How to Remove Spaces from a String in Python
What to look for: split(" ") only targets normal spaces. Tabs and newlines remain. If input can contain tabs, newlines, or mixed whitespace, use split() with no argument. It splits on any whitespace and removes extra runs automatically.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-spaces-from-a-string
Remove spaces from a string in Python - GeeksforGeeks
Explanation: s.strip() removes spaces from the start and end of s.
Published   July 11, 2025
🌐
Sololearn
sololearn.com › en › Discuss › 2055633 › pls-how-do-i-remove-white-space-between-two-words-in-python
Pls how do i remove white space between two words in ...
November 1, 2019 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-strip-trailing-whitespace-in-python
Trim a String in Python – How to Strip Trailing Whitespace
March 3, 2023 - Python has the rstrip() method you can use to do just that: fave_phrase = " Hello World " trimmed_fave_phrase = fave_phrase.rstrip() print(trimmed_fave_phrase) # output Hello World · In the example above, the leading space at the beginning ...
🌐
Accuweb
accuweb.cloud › home › how to remove spaces from a string in python (complete 2026 guide)
How to Remove Spaces From a String in Python (Complete 2026 Guide)
December 1, 2023 - That single line removes all whitespace characters from a Python string and is the most robust answer for most real-world use cases. ... Understanding the difference is critical for choosing the correct method. The replace() method replaces occurrences of a substring with another substring. To remove literal spaces: s = "W hi te space" newStr = s.replace(" ", "") print(newStr) Output: WhitespaceCopy
🌐
Medium
medium.com › @sourabhk087 › removing-unwanted-spaces-in-a-string-in-python-27004647d92a
Removing unwanted spaces in a string in python | by Sourabh | Medium
August 14, 2023 - It is a very basic method of removing extra unwanted spaces from the strings. It uses an inbuilt method of python that is split() and join() method to remove spaces. The process of this method is very simple.
🌐
Codingem
codingem.com › home › how to remove spaces from string in python
How to Remove Spaces from String in Python - codingem.com
July 10, 2025 - To remove spaces from a string in Python, use the str.replace() method. For example "This is a test".replace(" ", "") returns Thisisatest.
🌐
YouTube
youtube.com › shorts › ShIjFKXnB-w
Remove Spaces from String in Python in 1 Line ✂️
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-remove-multiple-empty-spaces-from-string-list
Python - Ways to remove multiple empty spaces from string List - GeeksforGeeks
July 12, 2025 - We split each string in the list into words using split(), which automatically removes any extra spaces and then joins them back into a single string with a single space between words using join().
🌐
W3Schools
w3schools.com › python › ref_string_strip.asp
Python String strip() Method
Python Examples Python Compiler ... Plan Python Interview Q&A Python Bootcamp Python Training ... The strip() method removes any leading, and trailing whitespaces....
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › remove-spaces-from-a-given-string
Remove spaces from a given string - GeeksforGeeks
April 11, 2026 - def removeSpaces(s): n = len(s) i = 0 itr = 0 # convert to list # (strings are immutable in Python) s = list(s) # Iterate through the string while i < n: # Check if current character # is not a space if s[i] != ' ': # Copy the non-space character # to the new string s[itr] = s[i] itr += 1 i += 1 # Return only the modified part of the string # without any extra characters return "".join(s[:itr]) if __name__ == "__main__": s = "g eeks for ge eeks " print(removeSpaces(s)) C# using System; class GfG { static string removeSpaces(string s) { int n = s.Length; int i = 0, itr = 0; // convert string to
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › remove-extra-spaces-string
Remove extra spaces from a string - GeeksforGeeks
December 3, 2023 - If non-space character is a full stop, comma or a question mark, we also remove any preceding space before it.