How to Replace Whitespaces with Underscores

5/5 - (2 votes)

Problem Formulation and Solution Overview

In this article, you’ll learn how to replace whitespaces with underscores in Python.

To make it more fun, we have the following running scenario:

Bryan, an IT Instructor, has given his students a Python coding challenge: Take a famous quote and replace all whitespaces with underscores in four (4) ways: five (5) for extra points.

💬 Question: How would we write code to increment a Dictionary value in Python?

We can accomplish this task by one of the following options:


Method 1: Use string replace()

This method uses Python’s built-in string library and calls the replace() function to replace each whitespace with the underscore character.

orig_quote = 'The secret of getting ahead is getting started - Mark Twain'
new_quote = orig_quote.replace(' ', '_')
print(new_quote)

Above, declares a famous quote from Mark Twain and saves it to orig_quote.

Next, replace() is called and passed two (2) parameters: the whitespace character to replace (' ') and the underscore character to replace it with ('_'). The results are saved to new_quote and output to the terminal.

The_secret_of_getting_ahead_is_getting_started_-_Mark_Twain
Python String Methods [Ultimate Guide]

Method 2: Use string join()

This method uses the join() and split() functions to split a string, by default, on the whitespace and re-join with the specified character to create a new string.

orig_quote = 'The journey of a thousand miles begins with one step - Lao Tzu'
new_quote = "_".join(orig_quote.split())
print(new_quote)

Above, declares a famous quote from Lao Tzu and saves it to orig_quote.

Next, join() is called and passed the argument orig_quote.split(). This argument splits orig_quote on the whitespace character (' ') by default and is replaced with the underscore character ('_'). The string is re-joined, saved to new_quote and output to the terminal.

The_journey_of_a_thousand_miles_begins_with_one_step_-_Lao_Tzu

Method 3: Use regex sub()

This method calls in the regex library and uses re.sub() to replace each whitespace character with the underscore character.

If the regex library is not installed, click here for installation instructions.

import re 
orig_quote = 'That which does not kill us makes us stronger. - Friedrich Nietzsche'
new_quote = re.sub(r'\s+', '_', orig_quote)
print(new_quote)

Above imports the regex library.

The following line declares a famous quote from Friedrich Nietzsche and saves it to orig_quote.

Next, using regex, the string is scanned for each occurrence of a whitespace ('\s+'); once found, it is replaced with the underscore character ('_'). This action repeats for the entire string. The results are saved to new_quote and output to the terminal.

That_which_does_not_kill_us_makes_us_stronger._-_Friedrich_Nietzsche
Python Regex Sub - How to Replace a Pattern in a String?

Method 4: Use a for loop

This method uses a for loop to traverse through the string to search for and replace each whitespace character with the underscore character.

orig_quote = "Don’t tell people your plans - show them your results - Anonymous"
new_quote = ""

for i in range(len(orig_quote)):
    if orig_quote[i] == ' ':
        new_quote = new_quote + '_'
    else:
        new_quote = new_quote + orig_quote[i]
print(new_quote)    

Above, declares a famous quote and saves it to orig_quote. Next, the new_quote is declared to hold the modified quote.

The following code instantiates a for loop, which loops through and analyzes each character of orig_quote. Each time a whitespace character is encountered, it is replaced with the underscore character and appended to new_quote.

Finally, new_quote is opened to the terminal.

Don’t_tell_people_your_plans_-show_them_your_results-_Anonymous

Bonus: Modify a Flat-Text File

This Bonus area opens a flat-text file and replaces each whitespace character encountered with the underscore character.

Contents of quotes.txt

What you do not want done to yourself, do not do to others - Confucius
To be happy, we must not be too concerned with others - Albert Camus
The only impossible journey is the one you never begin - Tony Robbins
import re
contents = ''
with open(r'quotes.txt', 'r') as fp:
    for line in fp:
        line = re.sub('( )+', '_', line)
        contents += line
    print(contents)

Above imports the regex library.

The following declares a string variable contents to hold the contents of the modified file.

Next, the quotes.txt file is opened and each file line is read in. All whitespace characters found in the line are replaced with underscore characters and saved to line. Then line is then appended to contents declared earlier.

Then contents are output to the terminal.

What_you_do_not_want_done_to_yourself,do_not_do_to_others-Confucius T
To_be_happy,_we_must_not_be_too_concerned_with_others-Albert_Camus The_only_impossible_journey_is_the_one_you_never_begin-_Tony_Robbins

Summary

These methods of replacing the whitespace character with an underscore character should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!


Regex Humor

Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)