Search code examples
pythonslicetableofcontentschop

Chop off text when it reaches certain word in Python


I'm using Python and I have an HTML-code which I want to chop off as soon as it reaches the word "toc", but I can't figure out how to do, anyone having some nice ideas?


Solution

  • Without more information, something like the following works

    s = "some string toc remainder of string"
    
    s = s[:s.find('toc')]
    
    print s  # some string
    

    But be aware that this will catch words like "stock" -- so there's likely a better approach depending on your specifics.