How to strip a specific word from a string?

后端 未结 5 1099
醉话见心
醉话见心 2020-12-03 04:20

I need to strip a specific word from a string.

But I find python strip method seems can\'t recognize an ordered word. The just strip off any characters passed to the

5条回答
  •  抹茶落季
    2020-12-03 04:58

    A bit 'lazy' way to do this is to use startswith- it is easier to understand this rather regexps. However regexps might work faster, I haven't measured.

    >>> papa = "papa is a good man"
    >>> app = "app is important"
    >>> strip_word = 'papa'
    >>> papa[len(strip_word):] if papa.startswith(strip_word) else papa
    ' is a good man'
    >>> app[len(strip_word):] if app.startswith(strip_word) else app
    'app is important'
    

提交回复
热议问题