Stripping everything but alphanumeric chars from a string in Python

前端 未结 11 1422
不思量自难忘°
不思量自难忘° 2020-11-22 10:52

What is the best way to strip all non alphanumeric characters from a string, using Python?

The solutions presented in the PHP variant of this question will probably

11条回答
  •  一整个雨季
    2020-11-22 11:49

    Regular expressions to the rescue:

    import re
    re.sub(r'\W+', '', your_string)
    

    By Python definition '\W == [^a-zA-Z0-9_], which excludes all numbers, letters and _

提交回复
热议问题