By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,034 Members | 2,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,034 IT Pros & Developers. It's quick & easy.

isdigit( ) not supported in PythonWin?

P: 13
Hi people!

I'm working with a Telit gsm module and am using PythonWin. I am trying to use the function isdigit( ) to check if a string contains only digits. However, PythonWin doesn't seem to support this function.

Does anyone know what I can do to work around this problem? I really need this function and cannot find other alternatives...

Thanks in advance!

Cheers, Samantha
Jan 5 '09 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 2.5K+
P: 2,851

isdigit() is a string method and is available in PythonWin.
Expand|Select|Wrap|Line Numbers
  1. >>> s = '1234567890'
  2. >>> s.isdigit()
  3. True
  4. >>> s1 = '1234567890a'
  5. >>> s1.isdigit()
  6. False
  7. >>> 
Jan 5 '09 #2

P: 13
Hi BV,

Am I supposed to import any particular module to be able to use isdigit()? Because Telit's Easy Script in Python document suggests that this function is not supported. Also, I checked the library for string and isdigit() is not in there either.

I was thinking if I could write the function manually myself and put it in the string module. But how should this function be written?

Thanks a million

Cheers, Samantha
Jan 7 '09 #3

Expert Mod 2.5K+
P: 2,851

isdigit() is a string method and is built into Python (I am using 2.3). Following are a couple of functions that return True of all the characters in a string are digits and False if not.
Expand|Select|Wrap|Line Numbers
  1. def isdigit(s):
  2.     for c in s:
  3.         if not (48 <= ord(c) <= 57):
  4.             return False
  5.     return True
  7. print isdigit('123456789a')
  8. print isdigit('1234567890')
  9. print isdigit('Z1994.dhh5')
  11. print
  13. def isdigit(s):
  14.     try:
  15.         int(s)
  16.     except:
  17.         return False
  18.     return True
Example usage:
Expand|Select|Wrap|Line Numbers
  1. >>> isdigit('123456789a')
  2. False
  3. >>> isdigit('1234567890')
  4. True
  5. >>> 
Jan 7 '09 #4

P: 13

Thank you so much for your help. I'm quite new to Python so pardon me for being unable to distinguish between string 'methods' and functions. Thank you for your corrections.

Anyhows, I do realise that isdigit( ) is supposed to be inbuilt into Python. That's why I was surprised that an error returned when I ran that part of the code. Not sure why that happens to the Python that I'm using. But I got around the problem by manually writing the function that you provided above. It works as expected now.

Thanks a million once again! =)

Cheers, Samantha
Jan 8 '09 #5

P: 126
When you start python you will get a message containing youre python version. Like this:
Expand|Select|Wrap|Line Numbers
  1. Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
  2. [GCC 4.3.2] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> >>> dir("")
  5. ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Jan 8 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.