How to remove special characters from a string in Python

Special characters are non-alphabetic or non-numeric characters. They are often part of string. To remove special characters from string, we can use str.isalnum()
.
Using a loop, iterate through each character in a string. In a conditional statement, using str.isalnum()
, check whether character is alphanumeric or not. Add each alphanumeric character to a new string. Then the new string will be free of special characters.
a = "Python @ point! #*" new = "" print(a) for character in a: if character.isalnum(): new += character print(new)
Output:
Python @ point! #* Pythonpoint
Subscribe
Login
Please login to comment
0 Discussion