473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

We have string.isdigit(), why not string.isNumber()?

N00b question alert! I did a search for isdigit() in the group
discussion, and it didn't look like the question had been asked in the
first 2 pages, so sorry if it was...

The manual documentation says:
"isdigit( )

Return true if all characters in the string are digits and there is at
least one character, false otherwise.
For 8-bit strings, this method is locale-dependent. "

So it makes sense that something like 5.6 would return false. But what
if we want to make sure that our string is a valid number, ie decimals
included?

I know how to write a regexp or method or whatever to do this, my main
question is *why* something like an isNumber() method is not baked
into the class. Does such functionality exist somewhere else in the
standard library that I'm just missing?

Jun 27 '08 #1
3 11904
In article
<0d**********************************@l17g2000pri. googlegroups.com>,
MooMaster <nt*****@gmail.comwrote:
So it makes sense that something like 5.6 would return false. But what
if we want to make sure that our string is a valid number, ie decimals
included?
Just call int(x) or float(x) inside a try block and see if if it raises an
exception.
Jun 27 '08 #2
On Apr 30, 7:56 pm, MooMaster <ntv1...@gmail.comwrote:
N00b question alert! I did a search for isdigit() in the group
discussion, and it didn't look like the question had been asked in the
first 2 pages, so sorry if it was...

The manual documentation says:
"isdigit( )

Return true if all characters in the string are digits and there is at
least one character, false otherwise.
For 8-bit strings, this method is locale-dependent. "

So it makes sense that something like 5.6 would return false. But what
if we want to make sure that our string is a valid number, ie decimals
included?

I know how to write a regexp or method or whatever to do this, my main
question is *why* something like an isNumber() method is not baked
into the class. Does such functionality exist somewhere else in the
standard library that I'm just missing?
A string s is a valid number if float(s) does not raise a ValueError.
Jun 27 '08 #3
MooMaster <nt*****@gmail.comwrites:
I know how to write a regexp or method or whatever to do this, my main
question is *why* something like an isNumber() method is not baked
into the class.
Because that name wouldn't conform to PEP 8.

(Also, and more importantly, because it's more correct to use it as
input to creating a new object of the type you want, and catch the
exception if it fails.)

--
\ "Courage is resistance to fear, mastery of fear — not absence |
`\ of fear." —Mark Twain, _Pudd'n'head Wilson_ |
_o__) |
Ben Finney
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Josué Maldonado | last post by:
Hello list, That's my question, I can't figure out a way to know if a given string is digit, soemthing like this: ISDIGIT("ARWA") = False ISDIGIT("5334") = True If anyone know a way to get...
6
by: Erwin | last post by:
Suppose I have a string, char *str = "1 2 a f g < )" and I need to check each of the components in the loop using isalpha, isdigit, isspace. How can I do that? I tried to do using this code, but...
2
by: Jefe | last post by:
Hi Group Could you please explain to me what's the difference between Chars.IsDigit and Chars.IsNumber? Regards,
4
by: SeNTry | last post by:
Hi Everyone, My first post here as I just begin to learn programming in general and python in particular. I have all the noobie confused questions, but as I work thru the tutorials I'm sure...
35
by: pinkfloydhomer | last post by:
How do I check if a string contains (can be converted to) an int? I want to do one thing if I am parsing and integer, and another if not. /David
3
by: kathy | last post by:
I want to know what is the easy way to check if a string is a number or not? the number can be int, float, double, scientific,... what is the easy way for only interger?
19
by: mathieu | last post by:
Hello, I am trying to parse a string and I am surprised by the result(*) When using: sscanf(s, "%03u%c", &val, &f); If a number less than 3 digits is found, shouldn't sscanf report that ? ...
4
by: Anoop | last post by:
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 ...
1
by: ronaldgranados | last post by:
Hi, I developed an application that generates code to be dinamically execute with reflection. When I used reflection everything seems to work fine until I saw a handle and memory leak. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.