Remove Special Characters
Remove all non-alphanumeric characters from a string.
BeginnerTopic: String Programs
Python Remove Special Characters Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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
Understanding Remove Special Characters
We keep only alphanumeric characters and spaces, filtering out punctuation and symbols.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.