1. 程式人生 > >python基礎--str.split

python基礎--str.split

超出 基礎 兩個 clas 包含 -- class this 使用

string = This +is -a /string

process = string.split(-)
process1 = string.split(-)[-1]#-1和-2可能存在某種特殊意義,小於-2或者大於1就超出list大小來
process2 = string.split(-)[0]
process3 = string.split(-)[1]
process4 = string.split(-)[-2]#

print(process)
print(process1)
print(process2)
print(process3)
print(process4) ‘‘‘ [‘This +is ‘, ‘a /string‘] a /string This +is a /string This +is ‘‘‘

python中str.split()返回的是一個list,包含了分割後的各個部分,我這個例子裏面是根據‘-‘來分割,結果是兩個元素,所以直接使用就可以啦。

python基礎--str.split