Skip to content

How to Replace Spaces with Underscores in Python


Python Tutorial: Replacing Spaces with Underscores

Welcome to our Python tutorial where we will learn how to replace spaces with underscores in Python. This tutorial will provide detailed step-by-step instructions, executable sample codes, and explanations to help you understand the process.


Before we get started, make sure you have Python installed on your computer. You can download the latest version of Python from the official Python website 1. Once you have Python installed, you’re ready to go!

Method 1: Using the Replace() Function

The easiest way to replace spaces with underscores in Python is by using the replace() function. This function allows us to replace a specific character, in this case, a space, with another character, which is an underscore. Let’s take a look at the code:

my_string = "Hello World"
new_string = my_string.replace(" ", "_")

In the above code, we create a variable my_string that holds the original string. Then, we use the replace() function to replace all occurrences of a space with an underscore. Finally, we print the new string which now has spaces replaced by underscores.

Method 2: Using the Regular Expression Module

Another way to replace spaces with underscores is by using the re module, which provides support for regular expressions in Python. Here’s how you can use it:

import re
my_string = "Hello World"
new_string = re.sub(r"\s", "_", my_string)

In the above code, we first import the re module. Then, we use the sub() function from the re module, which replaces all occurrences of a pattern with a specified string. The pattern \s matches any whitespace character, including spaces. We replace it with an underscore and store the result in the new_string variable. Finally, we print the updated string.

Method 3: Using List Comprehension

List comprehension is a concise way to create lists in Python. We can also use it to replace spaces with underscores in a string. Here’s how:

my_string = "Hello World"
new_string = "_".join([word for word in my_string.split()])

In the above code, we first split the original string using the split() function, which separates the string into a list of words. Then, we use list comprehension to iterate over each word and replace spaces with underscores. Finally, we use the join() function to join the words back into a single string using underscores as separators.


In this tutorial, we have learned three different methods to replace spaces with underscores in Python. Whether you prefer to use the built-in replace() function, the regular expression module, or list comprehension, you can now easily modify your strings to replace spaces with underscores.

Remember, Python provides countless possibilities for string manipulation, and these methods are just a starting point. Now it’s time to experiment and explore other exciting features of Python!

Let us know if you found this tutorial helpful or if you have any questions. Happy coding!

Keywords: replace spaces with underscores python, detailed Python tutorial, step-by-step Python code, executable sample codes, Python string manipulation, Python replace() function, Python regular expression module, Python list comprehension.


  1. Official Python website: