Python Replace Space With Underscore In String (4 Ways)

Python replace space with underscore in string

In this tutorial, we will be solving a program of python replace space with underscore in string. We will be discussing the solution to replace space with underscore in string in 4 ways.

Method 1 – Python Replace Space With Underscore In String using replace()

The simplest way to replace space with underscore in string in python is by using the python string replace() method. The replace() method will replace all the spaces with an underscore.

Syntax for replace() method:

string.replace(old_character, new_character, count)

Parameters:

  • Old Character – The old character is the required parameter that needs to be passed. And for the given problem old character will be space.
  • New Character – The new character is the required parameter that needs to be passed. And for the given problem new character will underscore ( _ ).
  • Count – The count parameter is an optional parameter. It defines the number of times you want to replace an old character with a new character, by default it replaces all old characters with new ones.

Python Code:

my_string = "My Programming Tutorial"

# Using replace() method to replace space with underscore
new_string = my_string.replace(" ", "_")

print("String with space ->", my_string)
print("String with underscore ->", new_string)

Output:

String with space -> My Programming Tutorial
String with underscore -> My_Programming_Tutorial

Also Read:

Method 2 – Python Replace Space With Underscore In String using split() and join()

Another way to solve this problem is by using the python string() and join() method. The steps to replace with underscore in string are:

  • Split the string with space using the split() method.
  • Join the list of elements returned by the split() method with an underscore
  • Print the new string.

Python Code:

my_string = "My Programming Tutorial"

# Using split and join to replace space with underscore
new_string = "_".join(my_string.split(" "))

print("String with space ->", my_string)
print("String with underscore ->", new_string)

Output:

String with space -> My Programming Tutorial
String with underscore -> My_Programming_Tutorial

Method 3 – Python Replace Space With Underscore In String using for loop

We can replace space with underscore in string using for loop. We will iterate on every character using for loop and compare it with whitespace. And if it matches we will replace it with the underscore and concatenate the character in the new string.

Python Code:

my_string = "My Programming Tutorial"

new_string = ""
# using for loop
for char in my_string:
    if char == " ":
        new_string += "_"
    else:
        new_string += char

print("String with space ->", my_string)
print("String with underscore ->", new_string)

Output:

String with space -> My Programming Tutorial
String with underscore -> My_Programming_Tutorial

Method 4 – Python Replace Space With Underscore In String using Regex sub()

In Python Regular Expression or Regex is a special sequence of characters that helps to match or find the other strings. To know more about regex in python you can read here.

The sub() method is an in-build method in the regex module. The sub() function is used to replace the occurrences of a substring with another substring which is provided as an argument in the method.

Syntax

re.sub(old_substring, new_substring, main_string)

Python Code:

# importing regex module
import re
my_string = "My Programming Tutorial"

# Uing re.sub() to replace space with underscore
new_string = re.sub(" ", "_", my_string)

print("String with space ->", my_string)
print("String with underscore ->", new_string)

Output:

String with space -> My Programming Tutorial
String with underscore -> My_Programming_Tutorial

Conclusion

Above we have discussed python replace space with underscore in 4 Ways. We can replace space with underscore in python by using replace(), for loop, re.sub() method, and by using split() and join() methods.

Similar Posts