Python program to replace character in a string with a symbol

Python program to replace all occurrences of a character with a symbol in a string :

In this tutorial, we will learn how to replace all occurrences of a character with a different symbol in a string. 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 given 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. The 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 replace() method :

Python String comes with an inbuilt method called replace() for replacing all occurrences of a substring in a string with a different substring.

This method is defined as below :

string.replace(old_str,new_str,count)

Where,

  • string: The main string where we want to do the modification.

  • old_str: The substring that we want to replace. This substring should be available in the main String.

  • new_str: 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 old_str and new_str, 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 old_str with new_str. If old_str 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) :

Description :

  1. Take the input string from the user by using the ‘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 input_string variable.

  2. Get the character that we want to replace in the string. This value is stored in the variable c.

  3. 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.

  4. 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 modified_str variable.

  1. Print out the final string variable.

Sample Output :

python replace character with symbol in string

Similar tutorials :