pythonstringreplace

How to remove all characters before a specific character in Python?


I'd like to remove all characters before a designated character or set of characters (for example):

intro = "<>I'm Tom."

Now I'd like to remove the <> before I'm (or more specifically, I). Any suggestions?


Solution

  • The included Python re has a sub function. Just match all the chars up to I, then replace the matched chars with I.

    import re
    
    re.sub(r'^.*?I', 'I', stri)