在 python 中,使用 split() 来切割字符串:

    1. sentence = 'I am an English sentence'
    2. sentence.split() # ['I', 'am', 'an', 'English', 'sentence']
    3. section = 'Hi. I am the one. Bye.'
    4. section.split('.') # ['Hi', ' I am the one', ' Bye', '']
    5. 'aaa'.split('a') # ['', '', '', '']

    在 python 中,使用 join() 来切割字符串:

    1. ';'.join(['apple', 'pear', 'orange']) # 'apple;pear;orange'
    2. ''.join(['hello', 'world']) # 'helloworld'

    在 python 中,字符串可以做类似于 list 的操作,如索引和切片:

    1. # 遍历
    2. word = 'helloworld'
    3. for c in word:
    4. print(c)
    5. # 通过索引访问
    6. print (word[0])
    7. print (word[-2])
    8. word[1] = 'a' # Error, 字符串不能通过索引来修改
    9. # 切片
    10. print (word[5:7])
    11. print (word[:-5])
    12. print (word[:])
    13. # join
    14. ','.join(word) # 'h;e;l;l;o;w;o;r;l;d'