How to tell if string starts with a number with Python?
PythonStringDigitsPython Problem Overview
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?
Python Solutions
Solution 1 - Python
Python's string
library has isdigit()
method:
string[0].isdigit()
Solution 2 - Python
>>> string = '1abc'
>>> string[0].isdigit()
True
Solution 3 - Python
Surprising that after such a long time there is still the best answer missing.
The downside of the other answers is using [0]
to select the first character, but as noted, this breaks on the empty string.
Using the following circumvents this problem, and, in my opinion, gives the prettiest and most readable syntax of the options we have. It also does not import/bother with regex either):
>>> string = '1abc'
>>> string[:1].isdigit()
True
>>> string = ''
>>> string[:1].isdigit()
False
Solution 4 - Python
sometimes, you can use regex
>>> import re
>>> re.search('^\s*[0-9]',"0abc")
<_sre.SRE_Match object at 0xb7722fa8>
Solution 5 - Python
Your code won't work; you need or
instead of ||
.
Try
'0' <= strg[:1] <= '9'
or
strg[:1] in '0123456789'
or, if you are really crazy about startswith
,
strg.startswith(('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'))
Solution 6 - Python
This piece of code:
for s in ("fukushima", "123 is a number", ""):
print s.ljust(20), s[0].isdigit() if s else False
prints out the following:
fukushima False
123 is a number True
False
Solution 7 - Python
You can also use try...except
:
try:
int(string[0])
# do your stuff
except:
pass # or do your stuff
Solution 8 - Python
Here are my "answers" (trying to be unique here, I don't actually recommend either for this particular case :-)
Using ord() and the special a <= b <= c
form:
//starts_with_digit = ord('0') <= ord(mystring[0]) <= ord('9')
//I was thinking too much in C. Strings are perfectly comparable.
starts_with_digit = '0' <= mystring[0] <= '9'
(This a <= b <= c
, like a < b < c
, is a special Python construct and it's kind of neat: compare 1 < 2 < 3
(true) and 1 < 3 < 2
(false) and (1 < 3) < 2
(true). This isn't how it works in most other languages.)
Using a regular expression:
import re
//starts_with_digit = re.match(r"^\d", mystring) is not None
//re.match is already anchored
starts_with_digit = re.match(r"\d", mystring) is not None
Solution 9 - Python
Using the built-in string module:
>>> import string
>>> '30 or older'.startswith(tuple(string.digits))
The accepted answer works well for single strings. I needed a way that works with pandas.Series.str.contains. Arguably more readable than using a regular expression and a good use of a module that doesn't seem to be well-known.
Solution 10 - Python
You could use regular expressions.
You can detect digits using:
if(re.search([0-9], yourstring[:1])):
#do something
The [0-9] par matches any digit, and yourstring[:1] matches the first character of your string
Solution 11 - Python
Use Regular Expressions, if you are going to somehow extend method's functionality.
Solution 12 - Python
Try this:
if string[0] in range(10):