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?
string.startswith('1').s[0]ors[:1]but the solution of John Machin:if s.startswith(tuple('0123456789')). Moreover, this solution raises error when it happens that s is a sequence like a tuple or list, which cases produce the same result as if it was a string. - Another solution is a regex whose pattern is '\d(?=\D)' but use of regex is superfluous here.stringis a module in the standard library and probably shouldn't be used as a variable name. docs.python.org/2/library/string.html