user14037074
user14037074

Reputation:

How can I remove a phrase from a string but not the individual words

It is resolved, that was a lack of attention on my part. report = report.replace('contact sales', '') works since replace returns the new string and does not alter the original.

I want to remove some

contact sales

instances from a string but I do not want to remove

contact

or

sales

if they exist individually.

I tried: report.replace('contact sales', '') but it did not work. It detects the phrase but does not remove it. How do I get this done?

Upvotes: 0

Views: 54

Answers (1)

Ethan Zerad
Ethan Zerad

Reputation: 46

You can get this done by redefining the variable when replacing instances.

For example:

report = report.replace('contact sales', '')

The logic behind it is that the replace() function does not modify the string in place, it creates a new string with the replaced instances. That's why you need to redefine it.

Upvotes: 2

Related Questions