字符串的分割还有partition()这种方式。
partition(sep) -->(head,sep,tail)
从左向右遇到分隔符把字符串分割成两部分,返回头、分割符、尾三部分的三元组。如果没有找到分割符,就返回头、尾两个空元素的三元组。
s1 ="I'm a good sutdent."
#以'good'为分割符,返回头、分割符、尾三部分。
s2 = s1.partition('good')
#没有找到分割符'abc',返回头、尾两个空元素的元组。
s3 = s1.partition('abc')
print(s1)
print(s2)
print(s3)
结果如下:
I'm a good sutdent.
("I'm a ", 'good', ' sutdent.')
("I'm a good sutdent.", '', '')