Fix Python – How to tell if string starts with a number with Python?

Question

Asked By – Illusionist

I have a string that starts with a number (from 0-9)
I know I can “or” 10 test cases using startswith() but there is probably a neater solution

so instead of writing

if (string.startswith('0') || string.startswith('2') ||
    string.startswith('3') || string.startswith('4') ||
    string.startswith('5') || string.startswith('6') ||
    string.startswith('7') || string.startswith('8') ||
    string.startswith('9')):
    #do something

Is there a cleverer/more efficient way?

Now we will see solution for issue: How to tell if string starts with a number with Python?


Answer

Python’s string library has isdigit() method:

string[0].isdigit()

This question is answered By – plaes

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0