Search code examples
pythonstringstring-formatting

Get content of string before '+' character in python


INPUT:

s = 'Coated tablet + ALFUZOSIN HYDROCHLORIDE, Film-coated tablet + ALFUZOSIN HYDROCHLORIDE, Modified-release tablet + ALFUZOSIN HYDROCHLORIDE, Prolonged-release tablet + ALFUZOSIN HYDROCHLORIDE'

EXPECTED OUTPUT:

s = 'Coated tablet, Film-coated tablet, Modified-release tablet, Prolonged-release tablet'

For each string like this, how do I get the necessary output in Python so that all elements after the + doesn't come.


Solution

  • Do split on , and then on + and fetch item at index 0

    ', '.join([i.split("+")[0].strip() for i in s.split(",")])
    

    Output

    'Coated tablet, Film-coated tablet, Modified-release tablet, Prolonged-release tablet'