Q:

Write a Python code to remove all characters except a specified character in a given string

0

Write a Python code to remove all characters except a specified character in a given string.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

def remove_characters(str1,c):
    return ''.join([el for el in str1 if el == c])
text = "Python Exercises"
print("Original string")
print(text)
except_char = "P"
print("Remove all characters except",except_char,"in the said string:")
print(remove_characters(text,except_char))
text = "google"
print("\nOriginal string")
print(text)
except_char = "g"
print("Remove all characters except",except_char,"in the said string:")
print(remove_characters(text,except_char))
text = "exercises"
print("\nOriginal string")
print(text)
except_char = "e"
print("Remove all characters except",except_char,"in the said string:")
print(remove_characters(text,except_char))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now