473,480 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help in string.digits functions

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
4 12704
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8060
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
2
2866
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
8
5441
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
21
3956
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
10
14959
by: Reiner Merz | last post by:
Hi, I'm looking for advice on how to parse a timestamp string according to the ISO 8601 specification. For those unfamiliar with the standard, here's an example: 2003-09-09T23:00:00Z...
39
2556
by: gtippery | last post by:
Newbie-ish questions - I've been away from C for a _long_ time. It seems to me that there ought to be easier (or at least shorter) ways to do what this does. It does compile & run for me (with...
6
7591
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
7
15270
by: bcpkh | last post by:
Hello All I need to check a string to make sure it does not contain any non numeric characters, the problem that I face is that the string is fairly long, 2784601121574585949, strtol etc. can't...
2
1399
by: WP | last post by:
Hello, below is my very first python program. I have some questions regarding it and would like comments in general. I won't be able to get my hands on a good python book until tomorrow at the...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6732
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4472
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.