split string in to 2 based on last occurrence of a separator

后端 未结 3 1632
独厮守ぢ
独厮守ぢ 2020-12-12 21:48

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.

for eg: consider the

相关标签:
3条回答
  • 2020-12-12 22:12
    >>> "a b c,d,e,f".rsplit(',',1)
    ['a b c,d,e', 'f']
    
    0 讨论(0)
  • 2020-12-12 22:12

    You can split a string by the last occurrence of a separator with 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']
    
    0 讨论(0)
  • 2020-12-12 22:15

    Use rpartition(s). It does exactly that.

    You can also use rsplit(s, 1).

    0 讨论(0)
提交回复
热议问题