Updated 2022-05-21 1
Viewed 3 times

I am trying to get text from string in python that has space before and after it. For example:

   txt = '12. (Hi Guys) I am Poorvi {End}'
   '13. (Hi Guys) John Doe /End/' # should return John Doe
   '14. (Hi Guys) Jane #End#'  #should return Jane

The idea is to return the string 'I am Poorvi' since the text has spaces before and after it. The string would only follow this format 12. (Hi Guys) I am Poorvi {End}. I want to get the text which lies after the character ) and before the space that is present before {End}.

The main issue I am facing is how to detect the whitespace before the character { since { is just for this example, it can be anything.

The only thing that is fixed is that the starting character is the whitespace after ) and the ending character is the space before the text at the end.

It would be nice if someone can show me how to solve this or give some pointers. I am new to python, struggling with this.

🟢 Solution

This feels dirty, but:

def strip_string(string_object):
    start = string_object.index(")")+2
    end = string_object.index("End")-2
    return string_object[start:end]