Python 字串 rpartition() 方法
示例
搜尋單詞 "bananas" 的最後一次出現,並返回一個包含三個元素的元組
1 - "匹配"之前的所有內容
2 - "匹配"本身
3 - "匹配"之後的所有內容
txt = "I could eat bananas all day, bananas are my favorite fruit"
x = txt.rpartition("bananas")
print(x)
自己動手試一試 »
定義和用法
rpartition()
方法搜尋指定字串的最後一次出現,並將字串分割成一個包含三個元素的元組。
第一個元素包含指定字串之前的部分。
第二個元素包含指定字串。
第三個元素包含字串之後的部分。
語法
string.rpartition(value)
引數值
引數 | 描述 |
---|---|
value | 必需。要搜尋的字串 |
更多示例
示例
如果未找到指定值,rpartition() 方法將返回一個包含以下內容的元組:1 - 一個空字串,2 - 一個空字串,3 - 整個字串
txt = "I could eat bananas all day, bananas are my favorite fruit"
x = txt.rpartition("apples")
print(x)
自己動手試一試 »