Most Pythonic was to strip all non-alphanumeric leading characters from string

前端 未结 3 819
[愿得一人]
[愿得一人] 2021-01-29 11:52

For example

!@#123myname --> myname
!@#yourname!@#123 --> yourname!@#123

There are plenty of S.O. examples of \"most pythonic ways of re

3条回答
  •  轮回少年
    2021-01-29 12:31

    If you want to remove leading non-alpha/numeric values:

    while not s[0].isalnum(): s = s[1:]
    

    If you want to remove only leading non-alphabet characters:

    while not s[0].isalpha(): s = s[1:]
    

    Sample:

    s = '!@#yourname!@#'
    while not s[0].isalpha(): s = s[1:]
    print(s)
    

    Output:

    yourname!@#
    

提交回复
热议问题