It works like this:
sentence = ' Cats go meow '
" ".join(sentence.split())
Answer from Nils on Stack OverflowPls how do i remove white space between two words in python
Learn more efficiently, for free: · Introduction to Python More on sololearn.com
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
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
Is there a way to remove whitespace between characters in python?
import re regex = r"(/)\s+(#)" test_str = ( "// # [some comment]\n" "// # [some comment]\n" "// # [some comment]\n" "// # [some comment]" ) subst = r"\1\2" result = re.sub(regex, subst, test_str) print(result) https://regex101.com/r/sGjMQg/1 More on reddit.com
Remove extra spaces from string #coding #programming #python
04:33
🧹 Clean Text Like a Pro: Remove Extra Whitespace with Regex! ...
00:34
Removing All Whitespace from a Python String! #python #coding - ...
07:34
How to remove Empty Lines, Unwanted(Spaces, Characters, Symbols, ...
03:02
How To Remove Extra Spaces Between Strings | string.join() | Python ...
01:48
How to Remove All Extra Spaces in between Words || How to Delete ...
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
Mimo
mimo.org › tutorials › python › how-to-remove-spaces-from-a-string-in-python
How to Remove Spaces from a String in Python
If removing spaces makes text unreadable, use the normalization example to keep single spaces between words. Use .strip() to remove whitespace at the start and end. Use .replace(" ", "") to remove normal spaces everywhere. Use "".join(s.split()) to remove spaces, tabs, and newlines.
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:
-
replace method -> text_wo_spaces = text.replace(" ","")
-
join and split methods -> text_wo_spaces = ' '.join(text.split())
-
list generator + join -> text_wo_spaces = ' '.join([char for char in text if char != ' '])
-
filter + join methods -> text_wo_spaces = ' '.join(filter(lambda char : != ' ', text))
Top answer 1 of 5
5
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.
2 of 5
4
3 is not a list generator it is a list comprehension. the generator expression for your solution would be ''.join(c for c in text if c != ' ') The list comprehension method (#3) and the filter (#4) are the worst both for memory usage and speed. The others, including the genexp given here, are more or less the same. #1 is the most readable.
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:
w3resource
w3resource.com › python-exercises › re › python-re-exercise-39.php
Python: Remove multiple spaces in a string
July 22, 2025 - Write a Python program to remove multiple spaces from a string. ... import re text1 = 'Python Exercises' print("Original string:",text1) print("Without extra spaces:",re.sub(' +',' ',text1))
CodeFatherTech
codefather.tech › home › blog › how do you remove spaces from a python string?
How Do You Remove Spaces From a Python String? - CodeFatherTech
June 27, 2025 - There are multiple ways to remove spaces from a Python string. The simplest approach is to use the string replace() method. If the spaces to remove are just at the beginning and at the end of the string the strip() method also works fine.
AskPython
askpython.com › python › string › python-remove-spaces-from-string
Python Remove Spaces from String - AskPython
February 16, 2023 - Firstly, the split() method returns a list of words in the entire string using a delimiter. Then we need to use the join() method to concatenate them. ... def remove(string_input): return "".join(string_input.split()) # Driver Program string_input= ' S a f a ' print(remove(string_input)) ... In the above example, the translate() function removes all of the white-spaces from the string and results in a compact string as output.
Study Trigger
studytrigger.com › home › python: remove extra spaces between words in a file (solved)
Python: Remove Extra Spaces Between Words in a File (Solved) - Study Trigger
January 27, 2026 - The Magic of .split(): When you call .split() without any arguments, Python automatically ignores all consecutive whitespace (spaces, tabs, newlines) and just extracts the words. The List of Words: This gives us a clean list of words with no spaces included. The Join Strategy: We then use " ".join(wordlist) to put the words back together into a single string, with exactly one space between each word. Write to File: Finally, we save this “clean” string into a new file. def removeExtraSpaces(): f1 = open("data.txt", "r") content = f1.read() f1.close() wordlist = content.split() clean_content = " ".join(wordlist) f2 = open("clean.txt", "w") f2.write(clean_content) f2.close() print("Extra spaces removed!
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
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 - This process ensures that all strings in the list are cleaned of unnecessary spaces, leaving only one space between words. Using re.sub() with regular expressions allows you to efficiently replace multiple spaces in a string with a single space. It’s a powerful method for cleaning up text data by handling patterns of unwanted whitespace. ... import re s = ["Hello world", " Python is great ", " Extra spaces here "] # Remove multiple spaces using regex li = [re.sub(r'\s+', ' ', string.strip()) for string in s] print(li)
Scaler
scaler.com › home › topics › how to remove whitespace from string in python?
How To Remove Spaces from a String in Python? - Scaler Topics
February 14, 2024 - To remove all spaces from a string in Python, combining the split() and strip() functions offers a straightforward approach. Here's how it works: First, use the split() function to divide the string into a list of words or characters, effectively separating them based on whitespace.