Python Practice › String Programs › Program 21

Remove unwanted characters

EasyString Programs · 21 of 106

Write a Python program to remove every character that is not a letter or digit.

Advertisement
Input
"Prog^ra*m#ming", "Py(th)#@&on Pro$*#gram"
Output
Programming
PythonProgram

Approach

  1. Keep only alphanumeric characters.

Solution

Python
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.

More string programs

← All 106 String Programs

Leave a Comment