5

Python has the function for string to test whether all characters are digits: string.isdigit().

In the manual is written:

For 8-bit strings, this method is locale-dependent

How is this method locale-depedent? In what locales are there digits that are outside the 0-9 range?

Also, if this is locale dependent, does python have a method for checking it with a specific locale (i.e. only 0-9 digits).

Peter Smit
  • 27,696
  • 33
  • 111
  • 170
  • 3
    http://en.wikipedia.org/wiki/Glyphs_used_with_the_Hindu-Arabic_numeral_system#Symbols – interjay Jul 02 '12 at 14:05
  • I think to manual just means that the actual 8-bit character codes are locale-dependent, not that the characters aren't 0-9. – martineau Jul 02 '12 at 14:27

2 Answers2

4

CPython uses the C function "isdigit" for the is_digit method on strings (see stringobject.c). See this related thread: Can isdigit legitimately be locale dependent in C

Apparently, it has to do with superscript digits, like 0xB2 ('²'), 0xB3 ('³') and 0xB9 ('¹').

HTH

Community
  • 1
  • 1
Jordan Dimov
  • 1,268
  • 12
  • 26
1

does python have a method for checking it with a specific locale (i.e. only 0-9 digits).

The simplest way:

>>> '1' in '1234567890'
True
>>> 'a' in '1234567890'
False

Your can also check ord, it might be faster (isn't):

>>> ord('0') <= ord('a') <= ord('9')
False
>>> ord('0') <= ord('5') <= ord('9')
True
Kos
  • 70,399
  • 25
  • 169
  • 233
  • Looking a global function and calling it, three times, shouldn't be faster than loading a local and doing 10 character comparisons in C. –  Jul 02 '12 at 16:57
  • This only checks a character, not a whole string of digits. – Peter Smit Jul 02 '12 at 19:31
  • You can apply this to a sequence using `all(function(e) for e in seq)`. – Kos Jul 02 '12 at 20:13