Illusionist
Illusionist

Reputation: 5489

How to tell if string starts with a number with Python?

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

Answers (12)

ramiro
ramiro

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

jcomeau_ictx
jcomeau_ictx

Reputation: 38462

>>> string = '1abc'
>>> string[0].isdigit()
True

Upvotes: 36

eyquem
eyquem

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

PascalVKooten
PascalVKooten

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

One Face
One Face

Reputation: 427

You can also use try...except:

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

Upvotes: 2

user166390
user166390

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

John Machin
John Machin

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

bradley.ayers
bradley.ayers

Reputation: 38382

Try this:

if string[0] in range(10):

Upvotes: -5

Tovi7
Tovi7

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

Ilya Smagin
Ilya Smagin

Reputation: 6152

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

Upvotes: -2

kurumi
kurumi

Reputation: 25609

sometimes, you can use regex

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

Upvotes: 12

plaes
plaes

Reputation: 32736

Python's string library has isdigit() method:

string[0].isdigit()

Upvotes: 254

Related Questions