How to strip a specific word from a string?

233,087

Solution 1

Use str.replace.

>>> papa.replace('papa', '')
' is a good man'
>>> app.replace('papa', '')
'app is important'

Alternatively use re and use regular expressions. This will allow the removal of leading/trailing spaces.

>>> import re
>>> papa = 'papa is a good man'
>>> app = 'app is important'
>>> papa3 = 'papa is a papa, and papa'
>>>
>>> patt = re.compile('(\s*)papa(\s*)')
>>> patt.sub('\\1mama\\2', papa)
'mama is a good man'
>>> patt.sub('\\1mama\\2', papa3)
'mama is a mama, and mama'
>>> patt.sub('', papa3)
'is a, and'

Solution 2

Easiest way would be to simply replace it with an empty string.

s = s.replace('papa', '')

Solution 3

You can also use a regexp with re.sub:

article_title_str = re.sub(r'(\s?-?\|?\s?Times of India|\s?-?\|?\s?the Times of India|\s?-?\|?\s+?Gadgets No'',
                           article_title_str, flags=re.IGNORECASE)

Solution 4

If want to remove the word from only the start of the string, then you could do:

  string[string.startswith(prefix) and len(prefix):]  

Where string is your string variable and prefix is the prefix you want to remove from your string variable.

For example:

  >>> papa = "papa is a good man. papa is the best."  
  >>> prefix = 'papa'
  >>> papa[papa.startswith(prefix) and len(prefix):]
  ' is a good man. papa is the best.'

Solution 5

Providing you know the index value of the beginning and end of each word you wish to replace in the character array, and you only wish to replace that particular chunk of data, you could do it like this.

>>> s = "papa is papa is papa"
>>> s = s[:8]+s[8:13].replace("papa", "mama")+s[13:]
>>> print(s)
papa is mama is papa

Alternatively, if you also wish to retain the original data structure, you could store it in a dictionary.

>>> bin = {}
>>> s = "papa is papa is papa"
>>> bin["0"] = s
>>> s = s[:8]+s[8:13].replace("papa", "mama")+s[13:]
>>> print(bin["0"])
papa is papa is papa
>>> print(s)
papa is mama is papa
Share:
233,087
Zen
Author by

Zen

Updated on July 05, 2022

Comments

  • Zen
    Zen almost 2 years

    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 parameter.

    For example:

    >>> papa = "papa is a good man"
    >>> app = "app is important"
    >>> papa.lstrip('papa')
    " is a good man"
    >>> app.lstrip('papa')
    " is important"
    

    How could I strip a specified word with python?

    • Benjamin Bannier
      Benjamin Bannier almost 10 years
      Did you look at the documentation of S.lstrip? It does something completely different to what you want to do. You want to replace this string with nothing.
    • DSM
      DSM almost 10 years
      What do you want to happen to the word "papaya"?
    • thefourtheye
      thefourtheye almost 10 years
      @DSM I guess the output should be ya. Lets wait for OP to confirm
    • thefourtheye
      thefourtheye almost 10 years
      @zen Are you sure the accepted answer is fine? Try this print "papa is papa is papa".replace('papa', '') and if the output is fine with you, then the accepted answer is correct.
    • Ely Fialkoff
      Ely Fialkoff over 5 years
      Just wanted to add something. Beware that if you are attempting to strip the last word in a sentence then you will be left with a space at the end of the sentence and this may not be desired. For example if you did papa.replace('man', ''). You will end up with 'papa is a good ' (notice the space after 'good'.
  • Jacob Kudria
    Jacob Kudria almost 10 years
    You might want to also add a space after papa - I assume he doesn't want a leading space left in the string.
  • thefourtheye
    thefourtheye almost 10 years
    Try print "papa is papa is papa".replace('papa', '')
  • thefourtheye
    thefourtheye almost 10 years
    Try print "papa is papa is papa".replace('papa', '')
  • metatoaster
    metatoaster almost 10 years
    Got distracted and didn't finish up with the examples for using re, which will allow removal of leading/trailing spaces.
  • Shayan Shafiq
    Shayan Shafiq over 3 years
    @thefourtheye It returns ' is is '. All the three 'papa's are removed as what is actually needed. May I know what should one infer from your point?
  • Shayan Shafiq
    Shayan Shafiq over 3 years
    @metatoaster "papa.replace('papa', '').strip()" also removes the leading and trailing spaces.
  • Shayan Shafiq
    Shayan Shafiq over 3 years
    @JacobKudria "s = s.replace('papa', '').strip()" also serves the purpose.