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

Help in string.digits functions

P: n/a
Hi All

I am getting two different outputs when i do an operation using
string.digits and test.isdigit(). Is there any difference between the
two. I have given the sample program and the output

Thanks for ur inputs

Anoop

#1:
~~
import string

test='121206'

if test not in string.digits:
print "I am Not Digit"
else:
print "I am Digit"

#2:
~~
import string

test='121206'

if not test.isdigit():
print "I am Not Digit"
else:
print "I am Digit"

Output
~~~~~
#1:I am Not Digit
#2:I am Digit

Thnks and Rgds

Anoop

Jul 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mon, 2006-07-24 at 22:19 -0700, Anoop wrote:
Hi All

I am getting two different outputs when i do an operation using
string.digits and test.isdigit(). Is there any difference between the
two. I have given the sample program and the output

Thanks for ur inputs

Anoop

#1:
~~
import string

test='121206'

if test not in string.digits:
print "I am Not Digit"
else:
print "I am Digit"

#2:
~~
import string

test='121206'

if not test.isdigit():
print "I am Not Digit"
else:
print "I am Digit"

Output
~~~~~
#1:I am Not Digit
#2:I am Digit

Thnks and Rgds

Anoop

string.digits is the string constant '0123456789'

So your test, "if test not in string.digits:" will evaluate True because
'121206' is not in '0123456789'.

Whereas test.isdigit() returns true if all the characters in test are
digits.

So yes, there is a big difference between the two.

Regards,

John

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Jul 25 '06 #2

P: n/a

Anoop wrote:
Hi All

I am getting two different outputs when i do an operation using
string.digits and test.isdigit(). Is there any difference between the
two.
Your first sentence appears to answer that ..but yes, there's quite a
difference. Have you read the manual?
I have given the sample program and the output
There is a much better way to try out very small snippets of code than
putting them in a script: use the Python interactive prompt.
>>import string
string.digits
'0123456789'
>>'0' in string.digits
True
>>'9' in string.digits
True
>>'90' in string.digits
False
>>'90' in string.digits
False
>>'123' in string.digits
True
>>'oo' in 'Anoop'
True
>>'' in 'Anoop'
True
>>>
Manual:
"""
For the Unicode and string types, x in y is true if and only if x is a
substring of y. An equivalent test is y.find(x) != -1. Note, x and y
need not be the same type; consequently, u'ab' in 'abc' will return
True. Empty strings are always considered to be a substring of any
other string, so "" in "abc" will return True. Changed in version 2.3:
Previously, x was required to be a string of length 1.
"""
>>'12345'.isdigit()
True
>>''.isdigit()
False
>>'xyz'.isdigit()
False
>>'123xyz'.isdigit()
False
>>'123 '.isdigit()
False
>>' 123'.isdigit()
False

Manual:
"""
isdigit( )
Return true if all characters in the string are digits and there is at
least one character, false otherwise.
"""

HTH,
John

Jul 25 '06 #3

P: n/a
John McMonagle wrote:
On Mon, 2006-07-24 at 22:19 -0700, Anoop wrote:
Hi All

I am getting two different outputs when i do an operation using
string.digits and test.isdigit(). Is there any difference between the
two. I have given the sample program and the output

Thanks for ur inputs

Anoop

#1:
~~
import string

test='121206'

if test not in string.digits:
print "I am Not Digit"
else:
print "I am Digit"

#2:
~~
import string

test='121206'

if not test.isdigit():
print "I am Not Digit"
else:
print "I am Digit"

Output
~~~~~
#1:I am Not Digit
#2:I am Digit

Thnks and Rgds

Anoop


string.digits is the string constant '0123456789'

So your test, "if test not in string.digits:" will evaluate True because
'121206' is not in '0123456789'.

Whereas test.isdigit() returns true if all the characters in test are
digits.

So yes, there is a big difference between the two.

Regards,

John

Your first test could be rewritten to do what I think you're thinking
it should do like so:

import string

test='121206'

for ch in test:
if ch not in string.digits:
print "I am not all Digits"
break
else:
print "I am all Digits"

But isdigit() would be the better way.
Peace,
~Simon

Jul 25 '06 #4

P: n/a
Hi All

Thankyou verymuch for ur inputs.

Hope u might have come across the string deprecation thought of in
Python 3.0 as a result of which we need to use all of these as some
objects

For example : string.lower(str) needs to be some thing like
str.lower().

Can u tell me whether such a change in the common python would require
these string.digits to be changed. If so wat would be ur suggestions

Thanks and regards

Anoop

John McMonagle wrote:
On Mon, 2006-07-24 at 22:19 -0700, Anoop wrote:
Hi All

I am getting two different outputs when i do an operation using
string.digits and test.isdigit(). Is there any difference between the
two. I have given the sample program and the output

Thanks for ur inputs

Anoop

#1:
~~
import string

test='121206'

if test not in string.digits:
print "I am Not Digit"
else:
print "I am Digit"

#2:
~~
import string

test='121206'

if not test.isdigit():
print "I am Not Digit"
else:
print "I am Digit"

Output
~~~~~
#1:I am Not Digit
#2:I am Digit

Thnks and Rgds

Anoop


string.digits is the string constant '0123456789'

So your test, "if test not in string.digits:" will evaluate True because
'121206' is not in '0123456789'.

Whereas test.isdigit() returns true if all the characters in test are
digits.

So yes, there is a big difference between the two.

Regards,

John

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Jul 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.