Replace all non-alphanumeric characters in a string

前端 未结 4 583
谎友^
谎友^ 2020-12-23 02:39

I have a string with which i want to replace any character that isn\'t a standard character or number such as (a-z or 0-9) with an asterisk. For example, \"h^&ell`.,|o w

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 03:31

    Try:

    s = filter(str.isalnum, s)
    

    in Python3:

    s = ''.join(filter(str.isalnum, s))
    

    Edit: realized that the OP wants to replace non-chars with '*'. My answer does not fit

提交回复
热议问题