How to remove all special characters punctuation and spaces from string in Python

1 Answer

0 votes
import re

def remove_special_characters(string):
    return re.sub(r"[^a-zA-Z0-9]+", "", string)

string = "Python! 3.12.1 is a high-level, general-purpose programming language."

string = remove_special_characters(string)

print(string)



'''
run:

Python3121isahighlevelgeneralpurposeprogramminglanguage

'''

 



answered Dec 14, 2023 by avibootz
...