我想知道是否有任何内置的函数在 python 中将字符串分成2部分,基于最后出现的一个分隔符。
例如: 考虑字符串“ a b c,d,e,f”,在拆分分隔符“ ,”之后,我希望输出为
“ a b c d e”和“ f”。
我知道如何操作字符串以获得所需的输出,但我想知道在 python 中是否有任何内置函数。
>>> "a b c,d,e,f".rsplit(',',1) ['a b c,d,e', 'f']
Use rpartition(s). It does exactly that.
rpartition(s)
You can also use rsplit(s, 1).
rsplit(s, 1)
You can split a string by the last occurrence of a separator with rsplit:
rsplit
Returns a list of the words in the string, separated by the delimiter string (starting from right).
To split by the last comma:
>>> "a b c,d,e,f".rsplit(',', 1) ['a b c,d,e', 'f']