Convert string (without any separator) to list

前端 未结 9 1855
挽巷
挽巷 2020-12-09 17:09

I have a phone number(string), e.g. \"+123-456-7890\", that I want to turn into a list that looks like: [+, 1, 2, 3, -, ...., 0].

Why? So I can go iterate through t

9条回答
  •  爱一瞬间的悲伤
    2020-12-09 17:19

    You can use the re module:

    import re
    re.sub(r'\D', '', '+123-456-7890')
    

    This will replace all non-digits with ''.

提交回复
热议问题