Remove Special Characters

Remove all non-alphanumeric characters from a string.

PythonBeginner
Python
# Program to remove special characters from a string

s = input("Enter a string: ")

cleaned = "".join(ch for ch in s if ch.isalnum() or ch.isspace())

print("Cleaned string:", cleaned)

Output

Enter a string: hello@world! 123#
Cleaned string: helloworld 123

We keep only alphanumeric characters and spaces, filtering out punctuation and symbols.