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

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -