Remove the first character of a string

后端 未结 5 590
萌比男神i
萌比男神i 2020-12-07 21:38

I would like to remove the first character of a string.

For example, my string starts with a : and I want to remove that only. There are several occurre

5条回答
  •  执笔经年
    2020-12-07 22:13

    Your problem seems unclear. You say you want to remove "a character from a certain position" then go on to say you want to remove a particular character.

    If you only need to remove the first character you would do:

    s = ":dfa:sif:e"
    fixed = s[1:]
    

    If you want to remove a character at a particular position, you would do:

    s = ":dfa:sif:e"
    fixed = s[0:pos]+s[pos+1:]
    

    If you need to remove a particular character, say ':', the first time it is encountered in a string then you would do:

    s = ":dfa:sif:e"
    fixed = ''.join(s.split(':', 1))
    

提交回复
热议问题