Remove unwanted characters
Write a Python program to remove every character that is not a letter or digit.
"Prog^ra*m#ming", "Py(th)#@&on Pro$*#gram"
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()))Programming PythonProgram
Line by line
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.