473,699 Members | 3,186 Online
Bytes | Software Development & Data Engineering Community
+ 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 11919
In article
<0d************ *************** *******@l17g200 0pri.googlegrou ps.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
13789
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 that done, I'll appreciate the help.
6
3568
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 gives me a core dump. char *point; point = str; while (point != '\0') { if (isalpha(point)) // It does not work either: if
2
19440
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
1396
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 I'll find most my answers. This one is eluding me tho... I am working in the tutorials, writing scripts as presented and then modifying and expanding on my own to try to learn. I'm working with one that asks the user to 'guess a number I'm...
35
75200
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
9916
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
3462
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 ? Thanks Mathieu
4
12719
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 Thanks for ur inputs Anoop
1
1862
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. Researching in the web I found several news that explaine memory leaks and handle leaks with reflection. Now, I need to take a decision about how can I resolve this issue:
0
8623
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
9187
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...
1
8936
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
7776
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6540
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
4390
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...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2360
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
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.