split、rsplit、partition和rpartition的用法
text = "Hello, how are you?"
result = text.split(",") # 使用逗号作为分隔符进行分割
print(result)
——————['Hello', ' how are you?']
text = "I love apples and oranges and bananas"
result = text.rsplit("and", 2) # 使用"and"作为分隔符进行分割,最多分割两次
print(result)
——['I love apples ', ' oranges ', ' bananas']
text = "I have a cat"
result = text.partition("have")
print(result)
——('I ', 'have', ' a cat')
text = "I have a cat"
result = text.rpartition("a")
print(result)
——('I have ', 'a', ' cat')
|