In this tutorial, you will learn to replace characters in a string in Python.
Python includes the replace() function, which can be used to replace characters in a string. This function accepts two input arguments; to replace a character, simply enter the first argument as the character to be replaced and the second argument is the character to be replaced with.
Syntax:
replace(OldChar, NewChar, Count)
where,
OldChar = Character that you want to replace.
NewChar = Character that you want OldChar to be replaced with.
Count = This is an optional Parameter, represeting number
you want the character to be replaced.
How To Replace Characters In A String In Python?
Let us take various code examples of replacing characters in a String in Python.
1. Replace Multiple Instances Of Characters In a String
In this example, you will see how to use the replace function to replace multiple instances of the characters in a string.
#Initializing String newString = "You,are,on,Coduber.com" #Replacing x to r newString = newString.replace(',', ' ') #Printing The New String print(newString)
2. Replace Single Character In A String
If you want to replace a specific character in a string you can do that with the help of replace function. Substrings that have been replaced by a new one are returned in this method, but the original string is not altered in any way If the old string could not be located, a copy of the original string was returned instead.
Let us see the below code example for it.
#Initializing String newString = "Yau are on Coduber.com" #Replacing x to r newString = newString.replace('a', 'o', 1) #Printing The New String print(newString)
Output:
You are on Coduber.com.
Even though the substring has been replaced, the original string has remained intact as can be seen in the example. Because we used the count parameter and set it to 1, it only replaced the first occurrence of the character ‘a’ with ‘o’.
3. Replace All The Instances Of A Substring In A Given String
Let us see the code example on how to replace all instances of the substring in a given string.
#Initializing String newString = "You are here to learn JAVA." #Replacing x to r newString = newString.replace("JAVA", "Python") #Printing The New String print(newString)
Output:
You are here to learn Python.
4. Replace Only One Instance Of A SubString In A Given String
Let us see the code example for replacing only one instance of a substring in a given String in Python.
#Initializing String newString = "Carnegie, ASU, MIT, ASU, Stanford" #Replacing x to r newString = newString.replace("ASU", "CalTech", 1) #Printing The New String print(newString)
Output:
Carnegie, CalTech, MIT, ASU, Stanford
5. Replace a Character In A String at the Index
Let us see the code example for replacing a character in a string at the index in Python.
#Initializing String newString = "Learn Pythen" #Replacing x to r replacedString = newString[:10] + 'o' + newString[11:] #Printing The New String print(replacedString)
Output:
Learn Python

Wrap Up
I hope you understand the various problem statements that arise when you ask how to replace characters in a string in Python. I’ve listed five different approaches to this problem statement.
If you have a better solution than the one discussed above, or if I have missed something, please let me know in the comments section and I will gladly add it here.
Further Read: