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

后端 未结 9 784
醉梦人生
醉梦人生 2020-11-28 08:37

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

intro = \"<>I\'m Tom.\"

Now I\'d

相关标签:
9条回答
  • str.find could find character index of certain string's first appearance:

    intro[intro.find('I'):]
    
    0 讨论(0)
  • 2020-11-28 09:25

    I looped through the string and passed the index.

    intro_list = []
    
    intro = "<>I'm Tom."
    for i in range(len(intro)):
        if intro[i] == '<' or intro[i] == '>':
            pass
        else:
            intro_list.append(intro[i])
    
    intro = ''.join(intro_list)
    print(intro)
    
    0 讨论(0)
  • 2020-11-28 09:29

    Since index(char) gets you the first index of the character, you can simply do string[index(char):].

    For example, in this case index("I") = 2, and intro[2:] = "I'm Tom."

    0 讨论(0)
提交回复
热议问题