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

前端 未结 13 1705
一整个雨季
一整个雨季 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:28

    If you wish to read only access to the string you can use array notation directly.

    Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> t = 'my string'
    >>> t[1]
    'y'
    

    Could be useful for testing without using regexp. Does the string contain an ending newline?

    >>> t[-1] == '\n'
    False
    >>> t = 'my string\n'
    >>> t[-1] == '\n'
    True
    

提交回复
热议问题