🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-spaces-from-string
Effective Ways to Remove Spaces from Strings in Python | DigitalOcean
Master Python string manipulation by learning how to remove whitespace and manage spaces with techniques like strip(), replace(), and regex for cleaner data.
Discussions

Pls how do i remove white space between two words in python
Learn more efficiently, for free: · Introduction to Python More on sololearn.com
🌐 sololearn.com
13
5
November 1, 2019
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
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
🌐 r/learnpython
12
2
July 19, 2022
🌐
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
🌐
EyeHunts
tutorial.eyehunts.com › home › python remove extra spaces between words | example code
Python remove extra spaces between words | Example code
October 13, 2021 - Using re.sub() or split() + join() method to remove extra spaces between words in Python. Regular expressions need to import the re module..
🌐
GeeksforGeeks
geeksforgeeks.org › 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 ...
🌐
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.
Find elsewhere
🌐
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 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!
🌐
Finxter
blog.finxter.com › home › learn python blog › a simple guide to remove multiple spaces in a string
A Simple Guide To Remove Multiple Spaces In A String - Be on the Right Side of Change
February 5, 2022 - Python String join() Python String split() This method is one of the simplest methods to remove multiple spaces in a string. Using a while loop, we will check if there are multiple trailing spaces in the string.
🌐
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)
🌐
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)),”.”)...
🌐
Caasify
caasify.com › home › blog › master python string handling: remove spaces with strip, replace, join, translate
Master Python String Handling | Caasify
October 5, 2025 - Here’s the magic combo of split() and join(): ... As you can see, the split() method has done its job by breaking the string into words and removing all those unnecessary spaces, tabs, and newlines.
🌐
Syntx Scenarios
syntaxscenarios.com › home › python › how to remove spaces from a string in python?
How To Remove Spaces From a String in Python?
January 4, 2025 - In Python, one of the easiest ways to remove spaces from a string is by using the strip() method, which is a pre-defined function. The strip() method removes all leading spaces (spaces at the beginning of a string) and trailing spaces (spaces ...
🌐
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.