473,811 Members | 2,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find a number is a digit or not?

440 Contributor
Hi,

Without using list ,How to find a number is a digit or not?.Is there any global function is availabe in Python which says the number is a digit.

Thanks in advance
PSB
Mar 7 '07 #1
3 9889
bartonc
6,596 Recognized Expert Expert
Hi,

Without using list ,How to find a number is a digit or not?.Is there any global function is availabe in Python which says the number is a digit.

Thanks in advance
PSB
How 'bout:
>>> "a".isdigit ()
False
>>> "1".isdigit ()
True
>>> "123".isdig it()
True
>>>
>>> help(str)
Help on class str in module __builtin__:

class str(basestring)
| str(object) -> string
<snip>
Mar 7 '07 #2
bartonc
6,596 Recognized Expert Expert
Hi,

Without using list ,How to find a number is a digit or not?.Is there any global function is availabe in Python which says the number is a digit.

Thanks in advance
PSB
I often wrap this in a try block:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     a = int(numStr)
  3. except ValueError:
  4.     a = 0
Mar 7 '07 #3
ghostdog74
511 Recognized Expert Contributor
isdigit() is usually the best way to check for digit.
you also can use re to check.
eg
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> a = "123"
  3. >>> if re.findall("\D+",a):
  4. ...  print "Not digit"
  5. ... else:
  6. ...  print "digit"
  7. ...
  8.  
Mar 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

18
22623
by: Bern | last post by:
how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
1
1452
by: Maq | last post by:
What I must to change in: #include <vector> #include <_str.h> #include <stdio.h> #include <ctype.h> void f_1(int a, int b) { printf ("f_1\n"); printf ("%d",b); scanf ("%d",&a);
3
9925
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?
5
2595
by: MN | last post by:
Hello, I have a customer table and another table that I need to prepopulate with special customer IDs, unique and not sequential. Is there a way to configure Access to assign the customer ID to every record each time that a record is either created for the first time or imported for the first time? I would be importing into a temp table and then running an update query to copy the data to the customer table. Another confusing issue is...
14
16231
by: dharmdeep | last post by:
Hi friends, I need a sample code in C which will convert a Hexadecimal number into decimal number. I had written a code for that but it was too long, I need a small code, so request u all to provide me with small sample code for that.
17
7723
by: rhitz1218 | last post by:
Hi, I'm trying to create a function that will sort a number's digits from highest to lowest. For example 1000 - will become 0001 or 1234 to 4321
3
6014
by: Kris | last post by:
I need to create a valid mod10 or luhn number that can be attached to an invoice, I have found several examples showing how to validate a credit card number or validate a number using mod10/luhn. Example: invoice number = 700123 invoice check number 700123(checknumber) that check number should make the entire invoice number able to validate using mod10/luhn
5
2572
debasisdas
by: debasisdas | last post by:
A ten-digit number contains every digit from 0 to 9. The digits are arranged so that the number formed by the first two digits, reading from left to right, is divisible by 2, the number formed by the first 3 digits is divisible by 3, the first 4 by 4, and so on until the whole number is divisible by 10. can you please find the number ...!
2
2927
by: xp5 | last post by:
Hi, I'm trying to find the total number of digit for a user's input integer. I'm using the sizeof() function but keeps getting the size is 4 for some odd reason. For example sizeof(32333) is 5 but it gives me 4. The following is my code, and my sizeof() function is near the middle. Thank you in advance #include<stdio.h> int main(void)
0
10644
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
10379
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
10393
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
10124
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
7664
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
6882
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
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
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3863
muto222
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.