90

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?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Zen
  • 4,381
  • 5
  • 29
  • 56
  • 1
    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. – Benjamin Bannier May 15 '14 at 04:02
  • 1
    What do you want to happen to the word "papaya"? – DSM May 15 '14 at 04:05
  • 1
    @DSM I guess the output should be `ya`. Lets wait for OP to confirm – thefourtheye May 15 '14 at 04:05
  • 2
    @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. – thefourtheye May 15 '14 at 04:31
  • 1
    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'`. – Ely Fialkoff Sep 24 '18 at 15:15

9 Answers9

124

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'
metatoaster
  • 17,419
  • 5
  • 55
  • 66
  • 2
    Try `print "papa is papa is papa".replace('papa', '')` – thefourtheye May 15 '14 at 04:30
  • 1
    Got distracted and didn't finish up with the examples for using `re`, which will allow removal of leading/trailing spaces. – metatoaster May 15 '14 at 04:41
  • @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 Oct 05 '20 at 15:36
  • 1
    @metatoaster "papa.replace('papa', '').strip()" also removes the leading and trailing spaces. – Shayan Shafiq Oct 05 '20 at 15:39
  • @ShayanShafiq leading and trailing spaces associated with the word, specifically. The intent is to couple the replacement with the pattern; see the usage against `papa3` (you should have tested `papa3.replace('papa', '').strip()` instead). – metatoaster Dec 31 '22 at 01:56
19

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

s = s.replace('papa', '')
iamdev
  • 716
  • 4
  • 13
13

If we're talking about prefixes and suffixes and your version of Python is at least 3.9, then you can use these new methods:

>>> 'TestHook'.removeprefix('Test')
'Hook'
>>> 'BaseTestCase'.removeprefix('Test')
'BaseTestCase'

>>> 'MiscTests'.removesuffix('Tests')
'Misc'
>>> 'TmpDirMixin'.removesuffix('Tests')
'TmpDirMixin'
qyryq
  • 170
  • 2
  • 6
8

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.'
theQuestionMan
  • 1,270
  • 2
  • 18
  • 29
4

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)
bfontaine
  • 18,169
  • 13
  • 73
  • 107
3

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
Michael Strobel
  • 367
  • 5
  • 16
2

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'
egvo
  • 1,493
  • 18
  • 26
2

Check it:

use replace()
------------
var.replace("word for replace"," ")
-----------------------------------
one = " papa is a good man"

two = " app is important"

one.replace(" papa ", " ")

output=> " is a good man"

two.replace(" app ", " ")

output=> " is important
Sallar Rabiei
  • 630
  • 1
  • 12
  • 33
2

It is better to

  1. Split the words

  2. Join the ones we are interested in with an if statement (you can pass in multiple words to strip)

    sentence = "papa is a good man"

    ' '.join(word for word in sentence.split() if word not in ['papa'])

Giri
  • 21
  • 2
  • It's the really helpful solution if you want to prevent removing "papa" if it is a part of another word. For example, this solution will not replace "papaya" with "ya" which may be the problem if we use the replace() function. – volkut Mar 20 '23 at 18:57