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 - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -