Convert string (without any separator) to list

前端 未结 9 1857
挽巷
挽巷 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:29

    Instead of converting to a list, you could just iterate over the first string and create a second string by adding each of the digit characters you find to that new string.

    0 讨论(0)
  • 2020-12-09 17:39

    Make a list(your_string).

    >>> s = "mep"
    >>> list(s)
    ['m', 'e', 'p']
    
    0 讨论(0)
  • 2020-12-09 17:40

    A python string is a list of characters. You can iterate over it right now!

    justdigits = ""
    for char in string:
        if char.isdigit():
            justdigits += str(char)
    
    0 讨论(0)
提交回复
热议问题