在 python 中,使用 split() 来切割字符串:
sentence = 'I am an English sentence'sentence.split() # ['I', 'am', 'an', 'English', 'sentence']section = 'Hi. I am the one. Bye.'section.split('.') # ['Hi', ' I am the one', ' Bye', '']'aaa'.split('a') # ['', '', '', '']
在 python 中,使用 join() 来切割字符串:
';'.join(['apple', 'pear', 'orange']) # 'apple;pear;orange'''.join(['hello', 'world']) # 'helloworld'
在 python 中,字符串可以做类似于 list 的操作,如索引和切片:
# 遍历word = 'helloworld'for c in word:print(c)# 通过索引访问print (word[0])print (word[-2])word[1] = 'a' # Error, 字符串不能通过索引来修改# 切片print (word[5:7])print (word[:-5])print (word[:])# join','.join(word) # 'h;e;l;l;o;w;o;r;l;d'
