Python - Replace multiple characters at once Last Updated : 08 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Replacing multiple characters in a string is a common task in Python Below, we explore methods to replace multiple characters at once, ranked from the most efficient to the least.Using translate() with maketrans() translate() method combined with maketrans() is the most efficient way to replace multiple characters. Python s = "hello world" replacements = str.maketrans({"h": "H", "e": "E", "o": "O"}) res = s.translate(replacements) print(res) Explanation:maketrans() function creates a mapping of characters to their replacements.translate() method applies the mapping to the string, replacing all specified characters efficiently.This method is highly optimized and works best for large strings.Let's explore some more ways and see how we can replace multiple characters at once in Python Strings.Table of ContentUsing replace() method in a loopUsing regular expressions with sub()Using list comprehension with join()Using replace() method in a loopreplace() method can be used repeatedly to handle multiple replacements. Python s = "hello world" replacements = {"h": "H", "e": "E", "o": "O"} for old, new in replacements.items(): s = s.replace(old, new) print(s) Explanation:The replace() method handles one replacement at a time.Using a loop allows all specified characters to be replaced sequentially.While effective, this method may be slower due to repeated operations on the string.Using regular expressions with sub()Regular expressions provide a flexible way to replace multiple characters. Python import re s = "hello world" pattern = "[heo]" res = re.sub(pattern, lambda x: {"h": "H", "e": "E", "o": "O"}[x.group()], s) print(res) Explanation:The sub() method matches the pattern and replaces each match using a mapping.Regular expressions are powerful for complex patterns but introduce extra overhead.Best used when patterns are not straightforward.Using list comprehension with join()This method processes the string character by character and replaces specified ones. Python s = "hello world" replacements = {"h": "H", "e": "E", "o": "O"} res = "".join(replacements.get(char, char) for char in s) print(res) OutputHEllO wOrld Explanation:The get method of the dictionary checks if a character needs replacement.Characters not found in the dictionary remain unchanged.This method is less efficient for large-scale replacements due to character-wise iteration. Comment More infoAdvertise with us Next Article Python - Replace multiple characters at once M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python | Replace multiple occurrence of character by single Given a string and a character, write a Python program to replace multiple occurrences of the given character by a single character. Examples: Input : Geeksforgeeks, ch = 'e' Output : Geksforgeks Input : Wiiiin, ch = 'i' Output : WinReplace multiple occurrence of character by singleApproach #1 : Nai 4 min read Python | Replace characters after K occurrences Sometimes, while working with Python strings, we can have a problem in which we need to perform replace of characters after certain repetitions of characters. This can have applications in many domains including day-day and competitive programming. Method #1: Using loop + string slicing This is brut 5 min read Python - Replace Different Characters in String at Once The task is to replace multiple different characters in a string simultaneously based on a given mapping. For example, given the string: s = "hello world" and replacements = {'h': 'H', 'o': '0', 'd': 'D'} after replacing the specified characters, the result will be: "Hell0 w0rlD"Using str.translate( 3 min read Replace a String character at given index in Python In Python, strings are immutable, meaning they cannot be directly modified. We need to create a new string using various methods to replace a character at a specific index. Using slicingSlicing is one of the most efficient ways to replace a character at a specific index.Pythons = "hello" idx = 1 rep 2 min read Python - Remove N characters after K Given a String, remove N characters after K character. Input : test_str = 'ge@987eksfor@123geeks is best@212 for cs', N = 3, K = '@' Output : 'geeksforgeeks is best for cs' Explanation : All 3 required occurrences removed. Input : test_str = 'geeksfor@123geeks is best for cs', N = 3, K = '@' Output 2 min read Like