how to check whether raw_input is integer, string and date in python
Question: I want to check my user input whether it is integer, string or date.

    
    
    data1 = raw_input("data = ")
        print(data1)
    
        if isinstance(data1, datetime.datetime.strptime(data1, '%Y-%m-%d')):
    
            print("date it is")
    
        elif isinstance(data1, int):
    
            print("int it is")
    
        elif isinstance(data1, basestring):
            print("str it is")
    

But it is showing:

    
    
    time data '10' does not match format '%Y-%m-%d'
    


Answer:   1. [`datetime.datetime.strptime()`](https://docs.python.org/2/library/datetime.html#datetime.datetime) will raise `ValueError` if the string does not match the format.

  2. [`raw_input()`](https://docs.python.org/2/library/functions.html#raw_input) always return string object, so [`isinstance(data1, int)`](https://docs.python.org/2/library/functions.html#isinstance) always is `False` if you don't covert the `data1` to int object before check.

  3. I'd suggest use [`try...except`](https://docs.python.org/2/tutorial/errors.html#handling-exceptions) to catch the `ValueError` like the following code:
    
        import datetime
    
    data1 = raw_input("data = ")
    print(data1)
    
    try:
        datetime.datetime.strptime(data1, '%Y-%m-%d')
    
    except ValueError:
    
        try:
            int(data1)
    
        except ValueError:
            print("str it is")
    
        else:
            print("int it is")
    else:
        print("date it is")
    

Demo:

    
        kevin@Arch ~> python2 input_check.py 
    data = 2014-01-02
    2014-01-02
    date it is
    
    kevin@Arch ~> python2 input_check.py 
    data = 12
    12
    int it is
    
    kevin@Arch ~> python2 input_check.py 
    data = foobar
    foobar
    str it is
    

