Python program to replace single or multiple character,substring in a string
Python tutorial to replace a single or multiple character or substring in a string :
In this tutorial, we will learn how to replace single or multiple characters in a string in python. For example, if you want to replace all ‘l’ with ‘#’ in ‘Hello World’, it will become ‘He##o Wor#d’.
Python has one inbuilt method for the string class called replace() to replace a substring inside a string. The syntax of this method is as below :
It will replace the old substring with new substring in the string str. If no substring old is found in the string, it will return the same string. As the string is immutable, this method actually creates one different string and returns it.count is optional.
If you pass any value to the count, then it will do the replace operation for count times. For example, if you have passed 2 as count, then it will replace the first two occurrences of old sub-string. Let me show you an example of the use case of this method :
The source code is available here for all examples.
Replace a single character or substring in a string :
Let’s use the replace() method to replace a single character in a user input string :
As you can see, we have easily replaced one character of the given string with a different character. We can also use the same method to replace one substring in a string like below :
Replacing a single character or substring ‘n’ number of times :
Similar to the above examples, we can also pass the value of count to replace a character or substring for count number of times in a string. Let’s take a look :
As you can see above that only two Hello was replaced instead of all if we are passing the value of count as 2. You can try the same example with a single character instead of a substring.
Replacing multiple different characters or substring in a string :
Python doesn’t provide any method to replace multiple different characters or substring in a string. Instead, we can call the replace() method multiple times to do the replacement for different characters or substrings.
Here, we are storing all the old and new values in two lists. Then we are iterating these lists and replacing each character one by one. Sample output :
We have replaced two characters in the input string Hello World : e with E and o with O. First, we have inserted them in two lists old_char holds [e,o] and new_char holds [E,O]. Then we have iterated through these lists and replaced the elements in old_char with its corresponding element in new_char.
That’s it. You can also create one different method to do the whole replacement process.
We have learned how to replace a single or multiple character/substring in python using replace() method. You can try to implement this method in different scenarios like replace a substring in all files in a folder, copy a file by replacing few substrings etc.Go through the examples above and drop one comment below if you have any queries.
You might also like :
- Python 3 program to print invert right angle triangle
- Python program to exchange two numbers without using third number
- Python program to find larger string among two strings
- Python program to find the first odd Abundant or excessive number
- Python 3 program to convert a decimal number to ternary (base 3)
- Count number of digits in a number in Python