You don't need regular expressions. Python has a built-in string method that does what you need:

mystring.replace(" ", "_")
Answer from rogeriopvl on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › article › python-program-to-replace-the-spaces-of-a-string-with-a-specific-character
Python Program to Replace the Spaces of a String with a Specific Character
March 27, 2026 - In Python, spaces in a string can be replaced with specific characters using the replace() method. The replace() method substitutes all occurrences of a specified substring with a new substring.
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
python - Replace spaces in string - Code Review Stack Exchange
5 Minimum amount of spaces inserted in a given string, such that it gets composed only of elements from a given list More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
April 23, 2016
Replace single character in string with space
i have a string str = i am a good boy i want to write a regex that would replace my single letter character with space. is there any… More on reddit.com
🌐 r/learnpython
7
0
September 8, 2016
python - How to replace every space within a text - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. ... First off this is Python 3. These are the lines of code I have created. I am looking to replace each space with ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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))

🌐
Stack Abuse
stackabuse.com › bytes › replace-underscores-with-spaces-in-python
Replace Underscores with Spaces in Python
July 1, 2022 - In order to change the string stored ... ") >>> underscore_str 'hello world' Another way to replace all underscores with spaces is to use the string's split(substr) and join(str) methods....
🌐
LabEx
labex.io › tutorials › python-how-to-replace-multiple-whitespaces-in-a-python-string-417565
How to replace multiple whitespaces in a Python string | LabEx
One of the most elegant and efficient ways to replace multiple whitespaces in Python is using a combination of the split() and join() methods. This approach is both simple and powerful. split(): When called without arguments, this method splits ...
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › replace single character in string with space
r/learnpython on Reddit: Replace single character in string with space
September 8, 2016 - this will preserve number of spaces and just drop single letter words (oh well it will also drop the space between i and am.. but whatever..). >>> re.findall("\w+\s*", s) ['i ', 'am ', 'a ', 'good ', 'boy'] ... >>> s = "i am a good boy" >>> "".join([i for i in re.findall("\w+\s*", s) if len(i.strip()) > 1]) 'am good boy' ... That would complete your intial request, but would replace all "i"s in any string with a space.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to replace spaces in a python string with a specific character
5 Best Ways to Replace Spaces in a Python String with a Specific Character - Be on the Right Side of Change
February 26, 2024 - This method employs the string replace() function, which is built into Python’s standard library. It’s specifically designed to replace occurrences of a substring within a string with another specified substring.
🌐
w3resource
w3resource.com › python-exercises › re › python-re-exercise-23.php
Python: Replace whitespaces with an underscore and vice versa - w3resource
July 28, 2025 - Write a Python program to search for both spaces and underscores in a string and swap them using regex substitution.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-replace-spaces-with-underscores
How to replace Spaces with Underscores in Python | bobbyhadz
Call the replace() method on the string. Pass a string containing a space and a string containing an underscore to the method.
🌐
Python Guides
pythonguides.com › replace-multiple-spaces-with-a-single-space-in-python
Replace Multiple Spaces with a Single Space in Python
October 28, 2025 - I often use it when cleaning up imported text data or preparing strings for NLP (Natural Language Processing) tasks. If you want more control or need to handle complex spacing patterns, Python’s re module is your best friend. The re.sub() function lets you replace all occurrences of a pattern, ...
🌐
CodeFatherTech
codefather.tech › home › blog › can i replace multiple spaces with one space in a python string?
Can I replace multiple spaces with one space in a Python string?
December 8, 2024 - In the syntax above, occurrences of the pattern specified (first argument) are replaced by the string repl (second argument), in the string provided (third argument). Let’s see how this works with a simple example of code. And remember that the first thing to do is to import the re module. import re text = "Hello, from CodeFatherTech! Let's learn Python!" text_with_single_space = re.sub(' +', ' ', text) print(text_with_single_space)
🌐
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 - We will start with the approach that works in most cases and then look at other options to give you a more complete knowledge of the topic. Spaces, we are coming for you! The easiest approach to remove all spaces from a string is to use the Python string replace() method.
🌐
Medium
medium.com › @bugsanalyze › how-to-replace-whitespaces-with-underscore-without-using-replace-function-in-python-3b7de422add6
How to replace whitespaces in A String with underscore without using replace function in python? | by G vamsi venkatesh | Medium
August 9, 2022 - In the above syntax pos is the index at which we replace empty space with _. Below is the example code. string = 'Lets start coding in python' for pos in range(0, len(string)): if(string[pos]==' '): string = string[:pos] + '_' + string[pos+1:] print(string)
🌐
Tutor Python
tutorpython.com › python-replace-space-with-underscore
Python Replace Space with Underscore - Tutor Python
October 19, 2023 - By invoking the re.sub() function, we trigger the search and substitution process, which scans the input string for occurrences of the specified pattern and replaces them with the provided replacement string. As a result, all spaces are effectively replaced with underscores, ensuring the desired outcome. When simple substring replacements are insufficient in Python coding.
🌐
SheCodes
shecodes.io › athena › 12032-how-to-replace-spaces-with-dashes-in-a-python-string
[Python] - How to Replace Spaces with Dashes in a Python String
Learn how to use the `split()` and `join()` methods in Python to replace all spaces with dashes in a string.