# How can i check whether the input is a number or string

Hi all,
I was trying out to write a small prog to prompt the user input for numbers...
I used the isdigit func to check whether the input is a number or string... it works but this doesn't accept float as a number... how can i do to make it works for both integer and float?

while True:
n1 = input("Enter the first number: ")
if n1.isdigit() == False:
print("Invalid input!")
else:
n1 = int(n1)
break
###### Who is Participating?

Commented:
For your case, you may reverse your logic and use isalpha() member function.
0

Commented:
Here is an example for you

#!/usr/bin/python

str = "123456";  # Only digit in this string
print str.isdigit();

str = "this is string example....wow!!!";
print str.isdigit();
0

Commented:
Here is a link to the string doc you should really look at
http://docs.python.org/library/stdtypes.html
0

Commented:
Or you can use isnumeric instead of isdigit
0

Author Commented:
yes i know but it prints false for float...
0

Author Commented:
the isnumeric works the same as isdigit...
so i tried to reverse it...
it works for both integer and float but it gives an error if the string contains one or more integers and the rest are alphabet instead of printing "Invalid Input"...
Is there any other method to solve this prob?

while True:
n1 = input("Enter the first number: ")
if n1.isalpha() == True:
print("Invalid input!")
else:
n1 = float(n1)
break
0

Commented:
Well, you can certainly use RegEx if you want

from re import match

n.match("\d+(?:\.\d+)?")
0

Author Commented:
can you explain this more in details... and i couldn't find match attribute in my python ver 3.2.2... thanks:)
0

Commented:
Sorry, I am trying to do it but I have fever so I apologize.

I am trying to use the regex library

You will have to import re or just match

from re import match

Then you will have to use the match function.

If you say
import re

You will have to use re.match()

Also, my syntax was wrong.

Give me a second let me give you the solution
0

Commented:
You can also make it differently.

Just try to convert it to a float and catch the exception if it fails.
``````try:
n1 =  float(n1)
except ValueError:
print "cannot convert  %s  to float %s" % n1
``````
0

Author Commented:
>>>no = "123.456"
>>>from re import match
>>>no.match("\d+(?:\.\d+)?")
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
no.match("\d+(?:\.\d+)?")
AttributeError: 'float' object has no attribute 'match'
0

Commented:
Sorry for being so slow

Here is what I have so far.

>>> b = re.match("\d+(?:\.\d+)?", "171.1")
>>> b.group(0)
'171.1'
>>> b = re.match("\d+(?:\.\d+)?", "171.a")
>>> b.group(0)
'171'
0

Commented:
Here is something you may like

>>> a = "171.123"
>>> from re import match
>>> b = match("\d+(?:\.\d+)?", a)
>>> if a == b.group(0):
...    print "A number"
...
A number
>>> a = "171.12a"
>>> b = match("\d+(?:\.\d+)?", a)
>>> if a == b.group(0):
...    print "A number"
...
>>>
0

Commented:
At this time, I would suggest you to go with gelonida's solution and save yourself a lot of time.  Just make it a separate function like isFloat.  And check isnumeric and isFloat and then you will be all good to go.
0

Founder, Software Engineer, Data ScientistCommented:
0

Author Commented:
Sorry for the late... N thanks for all the suggestions!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.