Stripping everything but alphanumeric chars from a string in Python

前端 未结 11 1301
不思量自难忘°
不思量自难忘° 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:35

    >>> import re
    >>> string = "Kl13@£$%[};'\""
    >>> pattern = re.compile('\W')
    >>> string = re.sub(pattern, '', string)
    >>> print string
    Kl13
    

提交回复
热议问题