473,725 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
+ 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 12720
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.digi ts
'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'.isdig it()
True
>>''.isdigit( )
False
>>'xyz'.isdigit ()
False
>>'123xyz'.isdi git()
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(st r) 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
8087
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 =================================================================================== //class bigInt implements big integers by storing the digits of a big //integer in a private vector data member called digit. Since it uses a //vector the size of the big integer can be indefinitely large. This...
2
2888
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 a sequence of spaces; recursive function 2 uses input to display ascending sequence of digits; likewise, recursive function 3 uses input to display descending sequence of digits. I have not followed the instructions completely regarding the...
8
5477
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
21
3980
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 data that is in this field can vary. It's a list of mechanical equipment, and how it is designated varies based on how the customer has them labeled. For example, a list of their equipment might look like: CH-1 CH-2 CH-3
10
14992
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 A compact form of it can as well be without the minuses and
39
2604
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 PowerC, a 16-bit DOS compiler); if there are nonstandard or "accidentally-works" aspects, please let me know. {This is the sort of situation where if I knew *what* to Google for, I wouldn't *need* to... <grin>}
6
7609
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, Karthi
7
15290
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 process this because much bigger than long obviously. Is there a way to check this string without resorting to scanning through the entire string a character at a time?
2
1403
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 earliest. The program takes a string and sums all numbers inside it. I'm testing with the following string: "123xx,22! p1" which should yield a sum of 123 + 22 + 1 = 146. My solution to this exercise was to write a function that takes a string and...
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4517
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.