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 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 Remove Multiple Characters from a String in Python Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Letâs explore the different ways to achieve this in det 2 min read Python - Replace occurrences by K except first character Given a String, the task is to write a Python program to replace occurrences by K of character at 1st index, except at 1st index. Examples: Input : test_str = 'geeksforgeeksforgeeks', K = '@' Output : geeksfor@eeksfor@eeks Explanation : All occurrences of g are converted to @ except 0th index. Input 5 min read Multiple Indices Replace in String - Python In this problem, we have to replace the characters in a string at multiple specified indices and the following methods demonstrate how to perform this operation efficiently:Using loop and join()join() is a brute force method where we first convert the string into a list then we iterate through the l 3 min read Replace Multiple Lines From A File Using Python In Python, replacing multiple lines in a file consists of updating specific contents within a text file. This can be done using various modules and their associated functions. In this article, we will explore three different approaches along with the practical implementation of each approach in term 3 min read Python Program To Remove all control characters In the telecommunication and computer domain, control characters are non-printable characters which are a part of the character set. These do not represent any written symbol. They are used in signaling to cause certain effects other than adding symbols to text. Removing these control characters is 3 min read Like