Search code examples
pythonsearchfindsubstring

How to find a substring of text with a known starting point but unknown ending point in python


I have a long string of text. I want to condense that string at a certain point using a key word to indicate the start of my new string in Python. For example, my string is:

"Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street." I want the text from "New York" to the end of the text i.e. I need code to pull the substring "New York City. I work on Wall Street."

have = "Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street."
want = "New York City. I work on Wall Street."
key_phrase = "New York"

Any help would be much appreciated!


Solution

  • I believe the best way to do this would be with regex:

    import re
    
    have = "Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street."
    want = "New York City. I work on Wall Street."
    key_phrase = "New York"
    
    key_phrase_begins = re.search(key_phrase, have).span()[0]
    new_string = have[key_phrase_begins:]
    print(new_string) # Outputs: 'New York City. I work on Wall Street.'
    

    What this is doing is searching for your key_phrase, and the index position at which the key phrase begins within the string. Then it is using indexing to create the new string from where the key_phrase begins in the original string.