Python delete in a string

后端 未结 4 1612
长情又很酷
长情又很酷 2021-01-21 08:51

I have these 3 strings:

YELLOW,SMALL,STRETCH,ADULT,T21fdsfdsfs
YELLOW,SMALL,STRETCH,ADULT,Tdsfs
YELLOW,SMALL,STRETCH,ADULT,TD

4条回答
  •  时光说笑
    2021-01-21 08:59

    If you refer to string elements, you can utilize str.rsplit() to separate each string, setting maxsplit to 1.

    str.rsplit([sep[, maxsplit]])

    Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right, rsplit() behaves like split() which is described in detail below.

    >>> lst = "YELLOW,SMALL,STRETCH,ADULT,T"
    >>> lst.rsplit(',',1)[0]
    'YELLOW,SMALL,STRETCH,ADULT'
    >>> 
    

提交回复
热议问题