Reputation: 5489
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?
Upvotes: 144
Views: 202069
Reputation: 898
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.
Upvotes: 3
Reputation: 27585
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
Upvotes: 4
Reputation: 21451
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
Upvotes: 31
Reputation: 427
You can also use try...except
:
try:
int(string[0])
# do your stuff
except:
pass # or do your stuff
Upvotes: 2
Reputation:
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
Upvotes: 1
Reputation: 82944
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'))
Upvotes: 9
Reputation: 2953
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
Upvotes: 0
Reputation: 6152
Use Regular Expressions, if you are going to somehow extend method's functionality.
Upvotes: -2
Reputation: 25609
sometimes, you can use regex
>>> import re
>>> re.search('^\s*[0-9]',"0abc")
<_sre.SRE_Match object at 0xb7722fa8>
Upvotes: 12
Reputation: 32736
Python's string
library has isdigit()
method:
string[0].isdigit()
Upvotes: 254