python - Removing strings from other strings -


how remove part of string like

blue = 'blue ' yellow = 'yellow' words = blue + yellow if blue in words:     words = words - yellow 

that thought like?

edit: know thatit won't work want know work. since cant use '-' in str()

you can use str.replace exchange text empty string:

words = words.replace(yellow,'') 

note transform:

"the yellow herring" 

into simply:

"the herring" 

(the replacement happen anywhere in string). if want remove 'yellow' end of string, use .endswith , slice:

if blue in words , words.endswith(yellow):     words = words[:-len(yellow)] 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -