Last updated: Apr 8, 2024
Reading timeยท3 min
re.sub()
split()
and join()
for
loopTo replace spaces with underscores in Python:
replace()
method on the string.replace()
method will return a string with all spaces replaced by
underscores.my_str = 'bobby hadz com' result = my_str.replace(' ', '_') print(result) # ๐๏ธ 'bobby_hadz_com'
We used the str.replace()
method to replace spaces with underscores in a
string.
The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
If you need to replace all whitespace characters in a string with underscores,
use the re.sub()
method.
re.sub()
This is a three-step process:
re
module from the standard library.re.sub()
method to replace all whitespace characters with
underscores.import re my_str = 'bobby hadz com' result = re.sub(r"\s+", '-', my_str) print(result) # ๐๏ธ 'bobby-hadz-com'
The re.sub() method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.
The \s
character matches Unicode whitespace characters like [ \t\n\r\f\v]
.
The plus +
is used to match the preceding character (whitespace) 1 or more
times.
str.join()
and str.split()
methods.split()
and join()
This is a three-step process:
str.split()
method to split the string on each whitespace
character.join()
method on a string containing an underscore.join
method will join the words with an underscore separator.my_str = 'bobby hadz com' result = '_'.join(my_str.split()) print(my_str.split()) print(result) # ๐๏ธ 'bobby_hadz_com'
We used the str.split()
method to
split the string on each whitespace character.
my_str = 'bobby hadz com' # ๐๏ธ ['bobby', 'hadz', 'com'] print(my_str.split())
The str.split() method splits the string into a list of substrings using a delimiter.
If a separator is not provided, the method regards consecutive whitespace characters as a single separator.
my_str = 'bobby hadz com' # ๐๏ธ ['bobby', 'hadz', 'com'] print(my_str.split())
The last step is to use the str.join
method to join the list of words with an
underscore separator.
my_str = 'bobby hadz com' result = '_'.join(my_str.split()) print(result) # ๐๏ธ 'bobby_hadz_com'
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
The string the method is called on is used as the separator between elements.
for
loopYou can also use a for loop to replace the spaces in a string with underscores.
my_str = 'bobby hadz com' new_str = '' for char in my_str: if char == ' ': new_str += '_' else: new_str += char print(new_str) # ๐๏ธ bobby_hadz_com
We declared a new variable that stores an empty string and use a for
loop to
iterate over the original string.
On each iteration, we check if the current character is a space.
If the condition is met, we add an underscore to the new variable.
Otherwise, we add the current character to the variable.
You can learn more about the related topics by checking out the following tutorials: