Help in string.digits functions

A

Anoop

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
 
J

John McMonagle

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
 
J

John Machin

Anoop said:
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.

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.
"""
False

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

HTH,
John
 
S

Simon Forman

John said:
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
 
A

Anoop

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top