PolyGeo PolyGeo - 1 year ago 67
Python Question

Removing leading digits from string using Python?

I see many questions asking how to remove leading zeroes from a string, but I have not seen any that ask how to remove any and all leading digits from a string.

I have been experimenting with combinations of

, slice notation and regex without yet finding a method.

Is there a simple way to do this?

For example:

  • "123ABCDEF" should become "ABCDEF"

  • "ABCDEF" should stay as "ABCDEF"

  • "1A1" should become "A1"

  • "12AB3456CD" should become "AB3456CD"

Answer Source

A simple way could be to denote all digits with string.digits, which quite simply provides you with a string containing all digit characters '0123456789' to remove with string.lstrip.

>>> from string import digits
>>> s = '123dog12'
>>> s.lstrip(digits)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download