Post

Replace space with underscore in Python

In this tutorial, we will explore various ways to replace spaces with underscore using Python programming.

Replace space with underscore in Python

Introduction

Replacing spaces with underscores is one of those everyday string-manipulation tasks that comes up more often than you might expect. You will encounter it when generating URL slugs from page titles, creating file names from user input, normalising column headers in a data pipeline, building snake_case identifiers, or sanitising form data before it is stored in a database. Python gives you several built-in tools to accomplish this cleanly, and each approach has its own strengths.

In this tutorial we will walk through five practical methods to replace spaces with underscores in Python. For each method we will explain the concept, show fully annotated code, display the expected output, and discuss when that particular approach is the best choice. By the end you will be comfortable picking the right tool for the right situation.

What will you learn?

  • How Python string methods like replace(), split(), and join() work in practice.
  • How to use regular expressions (re module) for pattern-based replacement.
  • How to use list comprehensions for character-by-character transformation.
  • How to use translate() with maketrans() for high-performance substitution.
  • When to choose each approach depending on your requirements.

Let us see them one by one.


Method 1 — Replace space with underscore using the replace() method

The replace() method is built into every Python string. It scans the string from left to right, finds every occurrence of a specified substring, and swaps it with a replacement substring. This is the most straightforward approach and the one you should reach for first.

Syntax:

1
string.replace(old, new, count=-1)

The optional count argument limits how many replacements are made. If omitted, all occurrences are replaced.

Full Annotated Code

1
2
3
4
5
6
7
8
9
# Step 1: Declare a variable and store a string with spaces.
my_string = "I love python programming language."

# Step 2: Use replace() to swap every space (' ') with an underscore ('_').
# The original string is NOT modified — replace() returns a new string.
my_string = my_string.replace(' ', '_')

# Step 3: Print the result.
print(my_string)

Output

1
I_love_python_programming_language.

How It Works

replace(' ', '_') iterates over every character in the string. When it finds a space character (ASCII 32), it swaps it for an underscore. The method is case-sensitive and works on spaces only — it will not touch tabs (\t) or newlines (\n) unless you specifically replace those too.

When to use it: Method 1 is best when you simply want to replace all spaces with underscores and have no need for any additional filtering or pattern matching.


Method 2 — Replace space with underscore using split() and join()

This two-step technique first breaks the string into a list of words (splitting on whitespace), then reassembles the list with underscores as the separator. It is a classic Pythonic pattern.

Full Annotated Code

1
2
3
4
5
6
7
8
9
10
11
12
13
# Step 1: Store the original string.
some_string = "Some sample text for example."

# Step 2: split() with no arguments splits on any whitespace and
# automatically removes leading, trailing, and consecutive spaces.
words = some_string.split()
# words is now ['Some', 'sample', 'text', 'for', 'example.']

# Step 3: join() concatenates the list items, placing '_' between each pair.
result = '_'.join(words)

# Step 4: Print the result.
print(result)

Output

1
Some_sample_text_for_example.

How It Works

split() called without arguments is smarter than split(' '). It treats any run of whitespace (multiple spaces, tabs, newlines) as a single delimiter and strips leading and trailing whitespace automatically. This makes the split() + join() pattern especially useful when you are cleaning up messy user input that may contain irregular spacing.

When to use it: Method 2 is best when the input may have multiple consecutive spaces, leading spaces, or trailing spaces, and you want them all collapsed into a single underscore.


Method 3 — Replace space with underscore using a List Comprehension

A list comprehension gives you character-level control. You iterate over every character in the string, replace spaces individually, and join the result back into a string.

Full Annotated Code

1
2
3
4
5
6
7
8
9
10
11
12
# Step 1: Define the original string.
string_sample = "This text will replace whitespace with underscore"

# Step 2: Use a list comprehension to iterate over each character.
# If the character is a space, replace it with '_'; otherwise keep it as-is.
string_list = ['_' if x == ' ' else x for x in string_sample]

# Step 3: Join the list of characters back into a single string.
string_sample = ''.join(string_list)

# Step 4: Print the result.
print(string_sample)

Output

1
This_text_will_replace_whitespace_with_underscore

Compact One-Liner Version

1
2
3
sentence = "hello world from python."
result = ''.join(['_' if c == ' ' else c for c in sentence])
print(result)

How It Works

The list comprehension ['_' if x == ' ' else x for x in string_sample] builds a new list where each space is swapped for an underscore and every other character is kept unchanged. ''.join(...) then merges all the characters back into a single string. This approach gives you maximum flexibility — you can add multiple conditions inside the comprehension to handle different characters in different ways.

When to use it: Method 3 is best when you need custom per-character logic, such as replacing spaces with underscores AND converting uppercase letters to lowercase at the same time.


Method 4 — Replace space with underscore using re.sub()

The re module provides powerful pattern-based string replacement. re.sub(pattern, replacement, string) finds every match of the pattern in the string and replaces it with the replacement.

Full Annotated Code

1
2
3
4
5
6
7
8
9
10
11
import re  # Import the built-in regular expressions module.

# Step 1: Define the original string.
my_string = "Replacing space with underscore with regular expressions function"

# Step 2: Use re.sub() to replace one or more consecutive whitespace characters
# with a single underscore. The pattern \s matches any whitespace character.
result = re.sub(r'\s+', '_', my_string)

# Step 3: Print the result.
print(result)

Output

1
Replacing_space_with_underscore_with_regular_expressions_function

How It Works

The pattern r'\s+' means “one or more whitespace characters”. The \s class matches spaces, tabs, newlines, carriage returns, and form feeds. The + quantifier means “one or more”, so multiple consecutive whitespace characters are collapsed into a single underscore. If you only want to match a literal space (not tabs or newlines), use r' ' instead.

When to use it: Method 4 is best when you need flexible pattern matching — for example, replacing any whitespace character, collapsing multiple spaces, or performing more complex text transformations.


Method 5 — Replace space with underscore using str.translate() and str.maketrans()

translate() is the fastest way to replace individual characters in Python. Under the hood it uses a lookup table (a dictionary mapping character ordinals to replacement ordinals), which avoids the overhead of scanning for a substring.

Full Annotated Code

1
2
3
4
5
6
7
8
9
10
11
12
13
# Step 1: Define the original string.
data = "Fast character replacement in Python."

# Step 2: Build a translation table using maketrans().
# maketrans(old_chars, new_chars) maps each character in old_chars
# to the corresponding character in new_chars.
table = str.maketrans(' ', '_')

# Step 3: Apply the translation table to the string.
result = data.translate(table)

# Step 4: Print the result.
print(result)

Output

1
Fast_character_replacement_in_Python.

How It Works

str.maketrans(' ', '_') creates a dictionary {32: 95} — the Unicode code point of space (32) mapped to the Unicode code point of underscore (95). str.translate(table) then uses this dictionary to replace characters in a single O(n) pass with minimal overhead.

When to use it: Method 5 is best when performance matters — for example, when processing very large strings or running the replacement inside a tight loop.


Comparison of All Methods

MethodHandles Multiple SpacesBest For
replace()No (each space → one _)Simple everyday use
split() + join()Yes (collapses to one _)Cleaning messy input
List comprehensionNo (one-to-one)Custom per-char logic
re.sub()Yes (with \s+)Pattern-based rules
translate()No (one-to-one)High-volume processing

Customisation Ideas

  1. Lowercase slug — combine replace() with .lower(): title.lower().replace(' ', '-').
  2. Snake case converter — strip punctuation too: re.sub(r'[^a-zA-Z0-9]+', '_', text).strip('_').
  3. Batch processing — apply to a list: [s.replace(' ', '_') for s in string_list].
  4. File name sanitiser — combine translate() with a deletion map to remove illegal file name characters.
  5. Pandas column normaliserdf.columns = df.columns.str.replace(' ', '_').

Common Errors and Fixes

Error 1: The original string is unchanged after calling replace()
Python strings are immutable. replace() returns a new string; it does not modify in place.
Fix: my_string = my_string.replace(' ', '_').

Error 2: re.sub() throws error: nothing to repeat
The regex pattern is malformed.
Fix: validate your pattern with an online tool like regex101.com.

Error 3: split() + join() collapses intended multiple spaces
split() without arguments collapses all consecutive whitespace.
Fix: use replace(' ', '_') or re.sub(r' ', '_', text) to preserve multiple underscores.

Error 4: translate() cannot handle multi-character patterns
str.maketrans() works at the single-character level only.
Fix: use replace() or re.sub() for multi-character patterns.


FAQ

Q1: How do I replace only the first space?
A: Use replace() with the count argument set to 1:

1
2
3
s = "hello world from python"
result = s.replace(' ', '_', 1)
print(result)  # hello_world from python

Q2: How do I replace spaces AND make the string lowercase at the same time?
A: Chain the two methods:

1
2
3
title = "My Blog Post Title"
slug = title.lower().replace(' ', '_')
print(slug)  # my_blog_post_title

Q3: How do I apply the replacement to all keys of a dictionary?
A: Use a dictionary comprehension:

1
2
3
original = {"first name": "Alice", "last name": "Smith"}
normalised = {k.replace(' ', '_'): v for k, v in original.items()}
print(normalised)  # {'first_name': 'Alice', 'last_name': 'Smith'}

We hope this tutorial gave you a solid understanding of the different ways to replace spaces with underscores in Python. Start with replace() for everyday tasks, reach for re.sub() when you need pattern power, and use translate() when you need speed.

Output:

Conclusion

In this tutorial, we go through the different methods in python that are used to replace space with underscore in python string. We use the replace() method, the combination of split() and join() method, we also use for loop and if condition together, we use regular expressions with re.sub(), and an external package called pythonscholar-replace-space. This method can have different ways of implementation but the outcome will be the same.

We hope you like this tutorial and you can use this method for your projects and works.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.