146

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?

4
  • 2
    If the question is asked: "Is this too repetitive?", the chances are -- in a high level language -- the answer is "Why, yes, it sure is". Be lazy! Commented Apr 7, 2011 at 7:46
  • 64
    You missed string.startswith('1'). Commented Apr 7, 2011 at 8:40
  • 3
    @Illusionist As it is written, your question means that you want to detect the strings that begin with only ONE digit. If so, the only right answer among the following ones, are not the ones using s[0] or s[: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. Commented Aug 20, 2011 at 11:10
  • 5
    Just being pedantic: string is a module in the standard library and probably shouldn't be used as a variable name. docs.python.org/2/library/string.html Commented Mar 14, 2013 at 23:16

12 Answers 12

259

Python's string library has isdigit() method:

string[0].isdigit()
Sign up to request clarification or add additional context in comments.

3 Comments

Python2's string module doesn't have methods, and isdigit is a method of str and unicode objects.
@plaes: -1 because of the above PLUS it crashes if s == ""
Look at my answer which does not break for an empty string: string[:1].isdigit()
38
>>> string = '1abc'
>>> string[0].isdigit()
True

6 Comments

I'm not a python guy, so perhaps you can help me on this: will this function bomb on "", where there is no string[0]?
yep, it sure will. you could use (string or 'x')[0].isdigit() to fix it for '' or None
You could try string[:1].isdigit(), which will quit happily deal with an empty string.
because that's not my answer; mine is still better in the case where the programmer wishes an exception to be thrown on an empty string.
@jcomeau_ictx exactly right. never defensively program. if the string is unexpectedly blank, that should error out and shouldn't be automatically handled anyway. Some schools of thought say you should gracefully handle and catch exceptions that aren't critical as warning and keep the program going. I am not of this mindset for most use cases. Specificity in the functions and code you are writing helps mitigate the edge cases where you would need to code defensively more often.
|
32

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

Comments

12

sometimes, you can use regex

>>> import re
>>> re.search('^\s*[0-9]',"0abc")
<_sre.SRE_Match object at 0xb7722fa8>

2 Comments

overkill! Why use re when you can do it in the bultins
@JakobBowyer At least, it is good to know about it! Even though in this case, yes, regexes ARE overkill if some builtins can do the same.
10

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'))

5 Comments

I would have upvoted you if there was only the solution with startswith(a tuple) . But the solutions with strg[:1] have two inconveniences: they don't raise an error if s may happen to be a list of strings, and they produce True if the string begins with SEVERAL digits.
@eyquem they produce True if the string begins with SEVERAL digits So? Other answers don't check for that, because it wasn't a requirement that it had to check that other chars weren't numeric.
I am crazy for startswith and did implement it this way in my code. Thank you.
One more startswith possibility... startswith(tuple(str(i) for i in range(10)))
.isdigit() is preferred
5

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

2 Comments

That is ugly. Use s[:1].isdigit() or s and s[0].isdigit()
@John Machin What do you mean by 'ugly' ? Not readable ? I don't find your propositions here above more readable. s and s[0].isdigit() is even less readable in my opinion. Personally, I like the construct ... if ... else ... Taking account of your remark, I would just improve it like that: s[0].isdigit() if s!="" else False
3

You can also use try...except:

try:
    int(string[0])
    # do your stuff
except:
    pass # or do your stuff

Comments

3

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.

Comments

2

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

1 Comment

(1) Lose the ord() (2) Lose the ^ in the regex.
0

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

Comments

-3

Use Regular Expressions, if you are going to somehow extend method's functionality.

Comments

-5

Try this:

if string[0] in range(10):

2 Comments

This doesn't work at all. range(10) produces a list of integers, while string[0] is a string of length 1.
That doesnt work; the types dont match. But: if string[0] in map(str,range(10)) works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.