Python program to replace character in a string with a symbol

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

In this tutorial, we will learn how to replace all occurence of a character with a different symbol. First we will take all the inputs from user : String, character to replace and the symbol. To replace , we are using ‘replace()’ method . Let’s take a look :

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

Description :

1. Take the input string from the user using ‘input()’ method.
2. Get the character that should be replace for all occurence.
3. Get the symbol to replace with
4. Replace all occurance of the character in the string and save the modified string in ‘modified_str’.
5. Print out the result.

Sample Output :

Enter a string : Hello World !!
Enter a character you want to modify in the above string : l
Enter the symbol you want to replace with : $
Modified String is :  He$$o Wor$d !!

Enter a string : HHHHHHH
Enter a character you want to modify in the above string : H
Enter the symbol you want to replace with : *
Modified String is :  *******

Enter a string : New York
Enter a character you want to modify in the above string : z
Enter the symbol you want to replace with : *
Modified String is :  New York

Leave a Reply