In this tutorial, we will learn how to replace all occurrences of a character with a different symbol. First, we will take all the inputs from the user: String, character to replace and the symbol. Our program will replace the character with the symbol in the string.
It will replace all occurrences of the character, i.e. if the character is found 5 times in the string, it will replace all 5 occurrences.
Using a loop, we can iterate over a string. We can use one for loop and compare each character of the string one by one. String is immutable in Python. We can’t change a character in a string directly.
So, we need to create one different string using the characters of the provided string. During the iteration process, keep building the new string by joining the characters. If any replaceable character is found, replace it with the symbol and join it.
We can use the process described above but in this blog post, I will show you one different way to solve this problem.
Python String comes with an inbuilt method called_ replace()_ for replacing all occurrence of a substring in the string with a different substring.
This method is defined as below :
string: The main string where we want to do the modification.
oldstr:_ The substring that we want to replace. This substring should be available in the String.
newstr:_ The substring that would replace the old substring.
count: This is an optional variable. This is used to define the number of times of the replacement.
In our case, the oldstr_ and newstr_, both will be a character. Also, we are not going to use count as we need to replace all occurrences of the character.
It will return one new string by replacing the first count occurrence of oldstr_ with newstr. If _oldstr_ is not found in the string, it will return the same string.
Let’s take a look at the program
input_string = str(input("Enter a string : ")) //1 c = input("Enter a character you want to modify in the above string : ") //2 symbol = input("Enter the symbol you want to replace with : ") //3 modified_str = input_string.replace(c,symbol) //4 print("Modified String is : ",modified_str) //5
(The source code is available here) :
- Take the input string from the user using ‘input()’ method. We are converting the value returned by_ input()_ to String by wrapping it with_ str(). This value, i.e. the user input string is stored in the _inputstring_ variable.
- Get the character that we want to replace in the string. This value is stored in the variable c.
- Also, get the symbol that we want to use by replacing the character read in the above step. It is stored in the variable symbol.
- Replace all occurrences of the character in the string using the replace method. You can see that this method is taking only two arguments here: the first one is the character we want to replace and the second one is the symbol to use by replacing the characters. We are not using the third argument as we are replacing all characters in the string. The final string is stored in the modifiedstr_ variable.
- Print out the final string variable.
- Python swap case of each character of a string
- Python program to convert Unicode or ASCII value to a character
- Python program to count the words and characters in a string
- Python program to convert character to its ASCII value
- Python program to swap the first and the last character of a string
- Python program to remove special characters from all files in a folder