Python Practice › String Programs › Program 21
Remove unwanted characters
Write a Python program to remove every character that is not a letter or digit.
Input
"Prog^ra*m#ming", "Py(th)#@&on Pro$*#gram"
Output
Programming PythonProgram
Approach
- Keep only alphanumeric characters.
Solution
for text in ["Prog^ra*m#ming", "Py(th)#@&on Pro$*#gram"]: print("".join(ch for ch in text if ch.isalnum()))Output
Programming PythonProgram
Line by line
Line 2
Spaces are not alphanumeric either, so “Py… Pro…” becomes one word, matching the expected output.
Learn the concept
New to this topic? Read the lesson, then come back.