根据上次出现的分隔符将字符串拆分为 2 [英] split string in to 2 based on last occurrence of a separator

查看:38
本文介绍了根据上次出现的分隔符将字符串拆分为 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 python 中是否有任何内置函数可以根据分隔符的最后一次出现将字符串分成 2 部分.

I would like to know if there is any built in function in python to break the string in to 2 parts, based on the last occurrence of a separator.

例如:考虑字符串 "a b c,d,e,f" ,在拆分分隔符 "," 之后,我希望输出为

for eg: consider the string "a b c,d,e,f" , after the split over separator ",", i want the output as

"a b c,d,e" 和 "f".

"a b c,d,e" and "f".

我知道如何操作字符串以获得所需的输出,但我想知道 python 中是否有任何内置函数.

I know how to manipulate the string to get the desired output, but i want to know if there is any in built function in python.

推荐答案

使用 rpartition(s).正是如此.

你也可以使用rsplit(s, 1).

这篇关于根据上次出现的分隔符将字符串拆分为 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆