Playing with Strings and Characters
Deleting all the occurrences of the given single length character from the given string
Here, we are using functions to delete all the occurrences of the user given character from the user given string. The character is of length 1 and if the input length is greater than one, the string is given as output.
For example, if the input string is “banana” and the character is “a”, the result would be “bnn”.
If the input string is “banana” and the character is “an”, the output would be “banana”
Note: Loved the post? You too can publish your article on “Python for fun” which will be loved by millions. Contribute an article!
def delchar(s,c): if len(c)==1: rs=s.replace(c,"") #the .replace function is used in strings to replace one specified phrase with another phrase return(rs) else: return(s) s=input("Enter a word: ")# this takes the input string c=input("Enter a single character: ") #This takes the input character print(delchar(s,c))
View all comments