Search code examples
pythonregexstrip

How to extract text starting from a specific word in a string?


So I have tried to extract only the address from this string, but I'm having troubles with it. This is how the string looks like:

1040 S. Vintage Ave.
Building A Ontario, CA 91761
United States Phone: 9099725134 Fax: 9099065401

Web: http://www.aareninc.com

I want to extract only the text that comes before the word 'Phone', so only the address.

I've tried with strip('Phone') and then take the first element of an array but it gives me the first letter of that string.

address = contacts.strip('Phone')
print(address[0])

Solution

  • As @JonClements commented, the solution is:

    contacts.partition('Phone')[0]