Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I remove multiple words from a strting in python

I know that how do I remove a single word. But I can’t remove multiple words. Can you help me?
This is my string and I want to remove "Color:", "Ring size:" and "Personalization:".

string = "Color:Silver,Ring size:6 3/4 US,Personalization:J"

I know that how do I remove a single word. But I can’t remove multiple words. I want to remove "Color:", "Ring size:" and "Personalization:"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Seems like a good job for a regex.

Specific case:

import re

out = re.sub(r'(Color|Ring size|Personalization):', '', string)

Generic case (any word before :):

import re

out = re.sub(r'[^:,]+:', '', string)

Output: 'Silver,6 3/4 US,J'

Regex:

[^:,]+   # any character but , or :
:        # followed by :

replace with empty string (= delete)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading