Bachu Bachu - 19 days ago 5
Python Question

How can I get a after and before match in a string

I want to get a after and before match in a string.
Example string:

"Hello I am Jim!"


Search string:
"I am"


Before match :
"Hello"


After match :
"Jim!"


How can I achieve this ?

Answer

You can also use split()

>>> before, after = "Hello I am Jim!".split("I am")
>>> before
'Hello '
>>> after
' Jim!'