How to Replace Whitespaces with Underscore in Python [5 Methods]

This Python tutorial will explore various methods and teach you how to replace whitespaces with underscore in Python.

Keep reading this Python article if you want to learn mentioned five methods to replace whitespaces with underscores in Python string:

  • Using the replace() function
  • Using the re.sub() method
  • Using the split() and join() methods
  • Using for loop
  • Using map() with lambda function

The last method mentioned above will be helpful when you have multiple strings and want to remove spaces with an underscore in Python.

Replace whitespaces with underscore in Python using the replace() function

The replace() method is a built-in function in Python that replaces a substring with another string.

This is the complete Python program to replace whitespaces with underscores using the replace() function.

welcome_message = "We're so glad you've chosen to come work at McKesson"
updated_welcome_message = welcome_message.replace(' ','_')

print(updated_welcome_message)

Output:

We're_so_glad_you've_chosen_to_come_work_at_McKesson

Refers to the screenshot below, which is taken after the Python program gets executed:

How to Replace Whitespaces with Underscores in Python

How to replace whitespaces with underscore in Python using the re.sub() method

Python has a built-in package called re(Regular expression), which can be used to work with Regular Expression. We will use the sub() function of the re-module, as it replaces the matches with the text of your choice.

Here is the full code of Python to replace whitespaces with underscores using the re.sub() method.

import re

Thought = "Stay Positive\tBetter days are on their ways"
todays_thought = re.sub("\s", "_", Thought)
print(today_thought)

I have given the first argument as a pattern to search for (“\s”), the second is the string we want to replace it with (“_”), and the third is the Python string to perform the replacement on.

 todays_thought = re.sub("\s", "_", Thought)

Output: The output in the screenshot below after the Python code runs successfully.

Replace whitespaces with underscore using re.sub() method in Python

Replace whitespaces with underscores in Python using the split() and join() methods

Here, we split the string into a list of substrings at the occurrence of each space using the split() method in Python.

Then, using the join() method, we join the list of substrings with an underscore as a separator in Python, and this way, we can successfully replace whitespace with underscores in Python.

This is the full Python program to replace whitespaces with underscores using the split() and join() methods.

proverb = "Strike while the iron is hot"
american_proverb = proverb.split()
print(american_proverb)

commonly_used_proverb = "_".join(american_proverb)
print(commonly_used_proverb)

Output: Refers to the image presented below to see the result of the Python program to replace whitespaces with underscores using split() and join() methods.

How to Replace Whitespaces with Underscores using split() and join() methods in Python

How to replace whitespaces with underscores using for loop in Python

This is another method to replace whitespaces with underscore in Python using a for loop.

Refers to the below-mentioned Python’s full code to replace whitespaces with underscores using for loop.

Thought = "Sucess is a Journey Not a Destination"
motivational_thought = ""
for char in Thought:
    if char == " ":
        motivational_thought += "_"
    else:
        motivational_thought += char
print(motivational_thought)

I will iterate on every character using a for loop in Python and compare it with whitespace. If it matches, replace it with the underscore and concatenate the character in the new string.

for char in Thought:
    if char == " ":
        motivational_thought += "_"
    else:
        motivational_thought += char

Output: You can refer to the screenshot below to see the result after executing the Python program.

How to replace whitespaces with underscore in Python

Replace Whitespaces with Underscores using map() with lambda function in Python

This is another approach to replace whitespaces with underscores in Python when we have multiple strings.

Here, I have used the lambda function to apply the replace() method to each string in the list, replacing spaces with underscores.

This is the source code of Python to replace whitespaces with underscores using map() with lambda function.

original_strings = ["Strike while the iron is hot", "Hello world"]
modified_strings = list(map(lambda x: x.replace(" ", "_"), original_strings))

print(modified_strings)

Output:

['Strike_while_the_iron_is_hot', 'Hello_world']

Refer to the screenshot below for the result after executing the Python code.

Replace whitespaces with underscores using map() with lambda function

Conclusion

I hope the information in the above Python article helps you learn how to replace whitespaces with underscores in Python.

Here, I have discussed several methods with examples of replacing whitespaces with underscores in Python, such as using the replace() function, re.sub() method, split() and join() method, using for loop, and using map() with lambda function.

You may also like to read: