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

Illusionist picture Illusionist · Apr 7, 2011 · Viewed 113.8k times · Source

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?

Answer

plaes picture plaes · Apr 7, 2011

Python's string library has isdigit() method:

string[0].isdigit()