How to split a string into an array of characters in Python?

前端 未结 13 1241
一整个雨季
一整个雨季 2020-11-22 01:54

I\'ve tried to look around the web for answers to splitting a string into an array of characters but I can\'t seem to find a simple method

str.split(//)

13条回答
  •  误落风尘
    2020-11-22 02:16

    If you want to process your String one character at a time. you have various options.

    uhello = u'Hello\u0020World'
    

    Using List comprehension:

    print([x for x in uhello])
    

    Output:

    ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
    

    Using map:

    print(list(map(lambda c2: c2, uhello)))
    

    Output:

    ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
    

    Calling Built in list function:

    print(list(uhello))
    

    Output:

    ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
    

    Using for loop:

    for c in uhello:
        print(c)
    

    Output:

    H
    e
    l
    l
    o
    
    W
    o
    r
    l
    d
    

提交回复
热议问题