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

后端 未结 3 1653
独厮守ぢ
独厮守ぢ 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

    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']
    

提交回复
热议问题