Replace multiple characters Python
In this post, you will learn how to replace multiple characters using Python programming language. In the development process, we may come to a situation where we need to replace multiple characters. There are several ways to replace multiple characters in Python, here we have mentioned some of them.
Python replace multiple characters using replace() method
The replace() method of Python returns a copy of the string where all occurrences of a substring are replaced with another substring. The syntax of the replace() method is-
string.replace(old, new, count)
Here, old is the string you search for, new is the string to replace the old value with and count is the number of times you want to replace the old substring with the new substring. In the given example, we have replaced multiple characters from a string using a nested replace() method.
# Python code to
# Replace multiple characters at once
# with help of nested replace() method
# initializing string
test_str = "This is testing string"
# printing original string
print("The original string is : " + str(test_str))
replace_characters = {'i': 'A',
'e': 'B',
'n': 'C'}
# Loop over key-value pairs
for key,value in replace_characters.items():
test_str = test_str.replace(key, value)
# printing result
print("New string after replacement of characters : " + test_str)
Output of the above code-
The original string is : This is testing string
New string after replacement of characters : ThAs As tBstACg strACg
Replace multiple characters in Python using translate() + maketrans()
The translate() function of Python enables us to map the characters in a string, replacing those in one table with those in another. And, the 'maketrans()' function in the string module makes it easy to create the mapping table. The syntax of the Python translate() method is-
str.translate(table[, deletechars])
Here, the table is the required parameter. It is a translation table, created by using maketrans() function, and deletechars is the list of characters removed from the string. Here is the code to replace multiple characters in a string.
# Python code to
# Replace multiple characters at once
# with help of translate() + maketrans()
import string
# initializing string
test_str = "This is testing string"
# printing original string
print("The original string is : " + str(test_str))
# Using translate() + maketrans()
# Replace multiple characters at once
res = test_str.translate(test_str.maketrans("ien", "ABC"))
# printing result
print("The string after replacement of positions : " + res)
Output of the above code-
The original string is : This is testing string
The string after replacement of positions : ThAs As tBstACg strACg
Python replace multiple characters using regex function
We can also use a regular expression to match white space and eliminate them using the re.sub() function.
# Python code to
# Replace multiple characters at once
# with help of regex function
import re
# initializing string
test_str = "This is testing string"
# printing original string
print("The original string is : " + str(test_str))
replace_characters = {'i': 'A',
'e': 'B',
'n': 'C'}
# Replace multiple characters using regex
test_str = re.sub(r"[ien]",
lambda x: replace_characters[x.group(0)],
test_str)
print("New string after replacement of characters : " + test_str)
Output of the above code-
The original string is : This is testing string
New string after replacement of characters : ThAs As tBstACg strACg
Related Articles
Find the largest of three numbers in Python
Print multiplication table in Python
Python program to multiply two matrices
Convert string to int python
Python heap implementation
zip function in Python
Remove last element from list Python
Convert string to list Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Count occurrences of each character in string Python
Python program to input week number and print week day
Check if two strings are anagrams Python
Python capitalize first letter of sentence
Python raise keyword