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
🌐
Stack Abuse
stackabuse.com › bytes › replace-underscores-with-spaces-in-python
Replace Underscores with Spaces in Python
July 1, 2022 - The join method is called on a list of strings and joins them together into a single string, separating them with the given separator string. Here you can see how the split method creates a list out of the given string: >>> underscore_str = "hello_world" >>> underscore_str.split("_") ['hello', 'world']
Discussions

Replace space with underscore using python - Stack Overflow
I need some help, I have a a txt file with spaces between words, I want to replace the space with underscore. fileHandler = open('nog_rename_update.txt') for eachline in fileHandler: new_name = More on stackoverflow.com
🌐 stackoverflow.com
modelbuilder - Replace occasional Space with Underscore using Python parser of ArcGIS Field Calculator? - Geographic Information Systems Stack Exchange
I am creating a model which will give me two string values, one which reads exactly what is in a field and another which automatically replaces any Space with an "_". I have used Calculate Value to... More on gis.stackexchange.com
🌐 gis.stackexchange.com
How to replace space with underscores using a regex in EPLAN?
Simple character-by-character replacements generally don't need regex, but it can be used. Just replace " " with "_". https://regex101.com/r/kVg9Jn/1 More on reddit.com
🌐 r/regex
9
1
August 26, 2024
Replacing spaces in filenames with underscores('_')? How can it be done?
How are you iterating over files in your scripts? If you're using find, and piping the output to other commands, use -print0 (and appropriate flags for other commands, like -z for sort) to keep spaces and newlines from screwing things up. If you're getting filenames from expansions, like path/*, ensure you're always quoting variable expansions correctly, like"${var}" to keep the shell from breaking on spaces. I could write you a script to rename files to change spaces to underscores, but it's probably good practice instead to make your Bash scripts more resilient, so you don't have to make sure you run the rename script all the time. EDIT: a simple way to rename all files in the current directory the way you want (spaces to underscores) you can just do: for file in *; do mv "${file}" "${file// /_}" done As a one-liner: $ for file in *; do mv "${file}" "${file// /_}"; done But like above, it seems better to be resilient against those types of filenames. More on reddit.com
🌐 r/linuxquestions
12
2
April 12, 2016
🌐
Python Guides
pythonguides.com › replace-whitespaces-with-underscore-in-python
Python Replace Whitespaces With Underscore
August 25, 2025 - Another Pythonic way is to use the map() function with a small lambda expression. This is not the most common approach, but it’s a neat trick that I’ve used when working with functional-style code. ... # Example: Replace spaces with underscores using map() text = "Boston Housing Price Index" result = "".join(map(lambda x: "_" if x == " " else x, text)) print("Original:", text) print("Modified:", result)
🌐
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 replace whitespaces with an underscore and vice versa. ... import re text = 'Python Exercises' text =text.replace (" ", "_") print(text) text =text.replace ("_", " ") print(text) ... Write a Python program to replace all spaces in a string with underscores and then convert underscores back to spaces.
🌐
Java2Blog
java2blog.com › home › python › python string › replace space with underscore in python
Replace space with underscore in Python [4 ways] - Java2Blog
January 22, 2022 - The for loop can iterate over a string in Python. In every iteration, we will compare the character with whitespace. If the match returns true, we will replace it with an underscore character. ... We iterate over the string s using the for loop. If the character is a space, we concatenate _ ...
🌐
Tutor Python
tutorpython.com › python-replace-space-with-underscore
Python Replace Space with Underscore - Tutor Python
October 19, 2023 - It’s a common Python practice to manipulate strings, and there are a variety of situations where you might need to swap out certain characters for others. For instance, there are circumstances wherein it becomes necessary to substitute spaces with underscores. Such as when handling file names, URLs, or other scenarios where spaces are prohibited or likely to create complications. This aspect bears significant importance in such situations. The replace() method is a built-in function in Python that allows us to replace occurrences of a specified substring with another substring.
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-replace-underscore-with-space-in-pythonexample.html
How to Replace Underscore with Space in Python? - ItSolutionstuff.com
October 30, 2023 - # Declare String Variable myString = "Hello_This_is_ItSolutionStuff.com_This_is_awesome." # Python string replace underscore with space myString = myString.replace("_", " ") print(myString)
Find elsewhere
🌐
CodeSpeedy
codespeedy.com › home › replace space with underscore in python
Replace space with underscore in Python - CodeSpeedy
November 9, 2022 - The substring that fits the specified pattern is replaced using the re.sub() method. It may be used to replace any whitespace in a string with an underscore. ... Using a separator, the split() method will divide the text into a list.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-replace-spaces-with-underscores
How to replace Spaces with Underscores in Python | bobbyhadz
We used the str.replace() method to replace spaces with underscores in a string. The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to replace whitespaces with underscores
How to Replace Whitespaces with Underscores - Be on the Right Side of Change
July 22, 2022 - This method uses Python’s built-in string library and calls the replace() function to replace each whitespace with the underscore character.
🌐
YouTube
youtube.com › watch
Replace Space with Underscore in Python - YouTube
Replace Space with Underscore using Pythonhttps://codingdiksha.com/python-replace-space-with-underscore/#python----------------------------------------------...
Published: December 17, 2021
🌐
JetBrains
youtrack.jetbrains.com › issue › PY-9786 › Automatically-replace-spaces-with-underscores-when-naming-functions-methods
Automatically replace spaces with underscores when ...
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
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. This method is straightforward and easy to use for such a common task. ... This code snippet takes a string text and uses the replace() method to substitute all spaces with underscores, assigning the result to new_text.
🌐
Stack Overflow
stackoverflow.com › questions › 71401739 › replace-space-with-underscore-using-python
Replace space with underscore using python - Stack Overflow
I need some help, I have a a txt file with spaces between words, I want to replace the space with underscore. fileHandler = open('nog_rename_update.txt') for eachline in fileHandler: new_name =
🌐
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 this blog we will be discussing ... . I have a string as below. ... If found empty space at particular index replace the character by dividing into substrings and appending underscore ....
🌐
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 - Original: Hello World Python Modified: Hello-World-Python · Similarly, spaces can be replaced with underscores by passing '_' as the replacement character ? sentence = "This is a Python tutorial" modified_sentence = sentence.replace(' ', '_') ...
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-replace-whitespace-with-underscore-in-pythonexample.html
How to Replace Whitespace with Underscore in Python? - ItSolutionstuff.com
October 30, 2023 - # Declare String Variable myString = "Hello, This is ItSolutionStuff.com, This is awesome." # Python string replace whitespace with underscore myString = myString.replace(" ", "_") print(myString)
🌐
Reddit
reddit.com › r/regex › how to replace space with underscores using a regex in eplan?
r/regex on Reddit: How to replace space with underscores using a regex in EPLAN?
August 26, 2024 -

Hey, guys. I’m a total newbie when it comes to regex and have no idea what I’m looking at, so I’m asking for your help. How can I replace spaces with underscores using a regex in EPLAN?

Example string: "This is a test" --> "This_is_a _test"

I also have an image of something else I’ve done where I removed '&E5/' from the string so that only "011" was left.

In EPLAN:

Where there are a Source Text and Output Text, one can put RegEx expressions.

Solution:

🌐
GitHub
gist.github.com › kranthilakum › 7536042
Python script to replace underscores with spaces · GitHub
Python script to replace underscores with spaces. GitHub Gist: instantly share code, notes, and snippets.