How to Replace Multiple Characters in a String in Python?

The string is a sequence of characters, alphabets, or any numbers in Python. Python provides various built-in functions that perform different string operations, such as replacing, adding, removing, etc.

While dealing with strings in Python, sometimes users have to replace single or multiple characters of a string with new characters. To replace multiple characters, various functions are used in Python.

Method 1: Using replace() Function

In Python, the “replace()” function replaces occurrences of the substrings in a string with another substring.

Example 1: Replace Multiple Characters in a String

In the below-given code, the “replace()” function is used to replace multiple characters in a string:

Code:

str_value = "python guide, python examples, python test"
output = str_value.replace("python", "Java")
output1 = output.replace(",", " -")
print(output1)

In the above code:

  • The “replace()” function is used twice to replace all the occurrences of two different substrings in a string.
  • Firstly, the substring “python” was replaced by “Java,” and secondly, the “,” is replaced by “” in the above program.

Output:

The above output verified that a specific substring replaces multiple characters in a string.

Example 2: Replace Dictionary of Substring in String

We can also use “Dictionary”  for replacing multiple characters in a string at once. In the below code, the dictionary is used to store multiple replaced characters:

Code:

str_value = "python guide, python examples, python test"
replace = {'python': 'Java', ',': ' -', 'test': 'quiz'}
for key, value in replace.items():
     str_value = str_value.replace(key, value)

print(str_value)

In the above code:

  • The string with duplicate characters is stored in a variable “str_value”.
  • The dictionary containing the replaced substring characters is also initialized.
  • The “for” loop is used to iterate over the input dictionary named “replace” and get the value of keys using the “.items” function.
  • In the body of the “for” loop, the “replace()” function is used to replace all the specific characters with a defined substring.

Output:

The above output reveals that the substring “python” has been replaced with “Java”. The substring “test” and “,” was also replaced by “quiz” and “”.

Example 3: Replace Specific Characters in a String According to Occurrence Position

The specific character will also be replaced by specifying the occurrence position. In the example below, the occurrence parameter of the “replace()” function is used to replace specific characters:

Code:

str_value = "python guide, python examples, python test"
output = str_value.replace("python", "Java", 2)
print(output)

In the above code, the “replace()” function is used to replace “python” substring with “Java” for the first two occurrences only because the occurrence value is set as “2”.

Output:

The above snippet shows that the “python” has been replaced by “Java” for the first two occurrences only. However, the third occurrence of the substring “python” remains the same.

Method 2: Using re.sub() Function

The “re.sub()” function of the “regex” module is used to replace the numerous characters in a string. The below code displays the usage of the re.sub() function:

Code:

import re
str_value = "python guide, python examples, python test"
result = re.sub("python", 'Linux', str_value)
print(result)

In the above code:

  • The “re.sub()” function accepts a string that needs to be replaced as a first argument, i.e., “python”.
  • The “re.sub()” function accepts the “Linux” as a second argument which means that all the occurrences of the substring “python” will be replaced with the “Linux”.

Output:

The above output indicates that the substring “python” has been replaced with “Linux”.

Method 3: Using translate() and maketrans() Function

The “translate()” function is used to map the replaced characters in the input string. The “maketrans()” function creates a one-to-one mapping of characters/substrings in a translation table. The “translate()” and “maketrans()” functions are used to replace multiple characters in the below code example:

Code:

import string
str_value = "pythOn guide, pythOn examples, pythOn test"
print('Input String: ',str_value)
print('\nReplaced String: ', str_value.translate(str.maketrans("O", "o")))

In the above code:

  • The “maketrans()” function accepts the substring character that needs to be replaced in a string as a first argument.
  • The second argument will be the character value that comes in the place of replaced character.
  • The “translate()” function will utilize the resulting translation table of the “maketrans()” function and replaces the specific characters in a string with their corresponding mapped characters.

Note: This method will only replace a single character with multiple characters in the string. You can use the previous methods to replace a sequence of characters (i.e., word).

Output:

The uppercase letter “O” has been replaced by “o” in a string.

Conclusion

To replace multiple characters in a string, the “replace()”, “re.sub()” and the “translate()” along with the “maketrans()” functions are used in Python. The “replace()” function accepts two substrings as arguments and replaces the multiple occurrences of the first substring with the second substring. This function also replaces characters using a dictionary and counts occurrences. This post explained different built-in Python methods to replace more than one character in a string.