Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Very basic question. We have the code:

a = input("how old are you")

if a == string:
    do this

if a == integer (a != string):  
    do that

Obviously it doesn't work that way. But what is the easiest way to do this. Thanks for any answers in advance.

We could also say:

if string in a:
    do this
share|improve this question
 
I'm not quite sure what you're asking. What do you want? –  feralin Jul 3 at 16:44
 
"But what is the easiest way to do this".. Do what? –  arshajii Jul 3 at 16:44
2  
It's important to note that input is always going to return a string. What you seem to be looking for is a way to detect whether that string contains digits. Make sure you understand the distinction. –  acjohnson55 Jul 3 at 16:48
 
I'm sorry if didn't ask the question very well. Ashwini Chaudhary's answer was perfect. That's all I needed. I was just getting an error in my app everytime the user inputted a letter rather than a number so I wanted the input box to stay in a loop, which broke if the input was an integer. Sorry if the question was asked badly. –  Dennis Callanan Jul 3 at 16:56

3 Answers

up vote 8 down vote accepted

You can use str.isdigit and str.isalpha:

if a.isalpha():
   #do something
elif a.isdigit():
   #do something

help on str.isdigit:

>>> print str.isdigit.__doc__
S.isdigit() -> bool

Return True if all characters in S are digits
and there is at least one character in S, False otherwise.

help on str.isalpha:

>>> print str.isalpha.__doc__
S.isalpha() -> bool

Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
share|improve this answer

You can use a.isalpha(), a.isdigit(), a.isalnum() to check if a is composed of letters, numbers, or a combination of numbers and letters, respectively.

if a.isalpha(): # a is made up of only letters
    do this

if a.isdigit(): # a is made up of only numbers
    do this

if a.isalnum(): # a is made up numbers and letters
    do this

The Python docs will tell you in more detail the methods you can call on strings.

share|improve this answer
4  
input will always be a string –  computmaxer Jul 3 at 16:45
 
Yeah, you are right. –  jh314 Jul 3 at 16:56

Seen that you use input() in tour example you should know that input always give you a string. And you need to cast it to the correct type, EG: Int, or Float.

def isint(input):
    return input.isdigit()

def isfloat(input):
    try: 
        return float(input) != None;
    except ValueError: 
        return False;

def isstr(input):
    if not isint(input) and not isfloat(input):
        return True
    return False

print isint("3.14")
print isfloat("3.14")
print isstr("3.14")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.