How to remove all special characters, punctuation marks and spaces from a string in Python?

To remove all special characters, punctuation, and spaces from a string, iterate through the string and filter out all non-alphanumeric characters. For example:

 & gt; & gt ; & gt; string = "Hello $ #! People Whitespace 7331" & gt; & gt; & gt; `` .join (e for e in string if e.isalnum ()) `HelloPeopleWhitespace7331` 

Regular expressions can also be used to remove any alphanumeric characters. re.sub (regex, string_to_replace_with, original_string) will replace all non-alphanumeric characters with an empty string. For example,

 & gt; & gt; & gt; impo rt re & gt; & gt; & gt; re.sub (`[^ A-Za-z0-9] +`, ``, "Hello $ #! People Whitespace 7331") `HelloPeopleWhitespace7331` 

Get Solution for free from DataCamp guru