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?
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
Try this:
if string[0] in range(10):
You can also use try...except
:
try:
int(string[0])
# do your stuff
except:
pass # or do your stuff
>>> string = '1abc'
>>> string[0].isdigit()
True
sometimes, you can use regex
>>> import re
>>> re.search('^\s*[0-9]',"0abc")
<_sre.SRE_Match object at 0xb7722fa8>
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
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
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
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'))
Use Regular Expressions, if you are going to somehow extend method's functionality.
Source: Stackoverflow.com