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 :

str.replace(old, new [, count]) 

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 witan h example of the use case of this method :

Replace single character or substring in a string :

Let’s use the replace() method to replace a single chracter in a user input string :

user_input_str = input("Enter a string : ")
old_char = input("Enter a character to replace in this string : ")
new_char = input("Enter the new character : ")

new_str = user_input_str.replace(old_char,new_char)
print("New string is : ",new_str)

Sample output :

Enter a string : Hello World
Enter a character to replace in this string : e
Enter the new character : #
New string is :  H#llo World

python replace character or substring in 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 :

user_input_str = input("Enter a string : ")
old_sub_str = input("Enter a substring to replace in this string : ")
new_sub_str = input("Enter the new substring : ")

new_str = user_input_str.replace(old_sub_str,new_sub_str)
print("New string is : ",new_str)

Sample output :

Enter a string : Hello Hello Hello world
Enter a substring to replace in this string : Hello
Enter the new substring : Bye
New string is :  Bye Bye Bye world

python replace character or substring in string

Replacing 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 :

user_input_str = input("Enter a string : ")
old_sub_str = input("Enter a substring to replace in this string : ")
new_sub_str = input("Enter the new substring : ")
str_count = int(input("Enter number of replacement : "))

new_str = user_input_str.replace(old_sub_str,new_sub_str,str_count)
print("New string is : ",new_str)

Sample output :

Enter a string : Hello Hello Hello World
Enter a substring to replace in this string : Hello
Enter the new substring : Bye
Enter number of replacement : 2
New string is :  Bye Bye Hello World

python replace character or substring in string
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 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 chracters or substrings.

user_input_str = input("Enter a string : ")

old_char = []
new_char = []

total_char = int(input("How many charcters you want to replace : "))

for i in range(total_char):
    old_char.append(input("Enter a character you want to replace : "))
    new_char.append(input("Enter new value for this chracter : "))

for i in range(total_char):
    user_input_str = user_input_str.replace(old_char[i],new_char[i])

print("New string is : ",user_input_str)

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 :

Enter a string : Hello World
How many charcters you want to replace : 2
Enter a character you want to replace : e
Enter new value for this chracter : E
Enter a character you want to replace : o
Enter new value for this chracter : O
New string is :  HEllO WOrld

We have replaced two chracters 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.
python replace character or substring in string

Conclusion :

We have learnt 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 sub string 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 :

Leave a Reply