How to check user input data types in python? | applied electronics engineering

Latest

How to check user input data types in python?

By Applied Electronics - Monday, April 10, 2017 No Comments
In this python programming tutorial you will learn How to check user input data types in python? In other words, you ask user to input for example age and you expect it to be an integer. But what if the user did not enter integer but instead entered alphabets etc. How will you check and verify user input? So in this tutorial you will learn how to check that the user input is indeed integer or alphabets or both.

In the tutorial How to keep repeating a program until a specific input is obtained? we showed one simple way of obtaining required input from the users. It checked the user input against pre-stored name. If it is not the same name then the user is prompted again for his/her name.

But many times you want users to enter some data types like integers or integers and alphabets. How would you check that the user inputted integers or alphabets? This is easy with Python programming language.

To check whether the user input is integer you can use the isdecimal() function. To check whether entered alphabets you can use the isalpha() function and to check whether the user input is integer and alphabets you can use the isalnum() function.

To check that the user input is integer you can use the following code.

while True:
    age = input('Age: ')
    if age.isdecimal():
        break
    print('Please enter your age')

 We begin with a continuous while loop. In the while loop we ask user to input age and store it in a variable called age. Then using the object created age function isdecimal() using the dot notation we check whether the input is indeed an integer. If it is then we break out of the while loop. Otherwise the we stay inside the while loop and print Please enter your age continuously. 
The result of running this program is as follows.

Age: ad
Please enter your age

Age: effg
Please enter your age

Age: 20

In similar way we can use the isalpha() function to check whether user has entered alphabets when asking names or other similar inputs where alphabets are expected. Following is one code example which does this.

while True:
    name = input('Name: ')
    if name.isalpha():
        break
    print('Please enter your name')

The result of running this python code is as follows.

Name: 43
Please enter your name

Name: 76
Please enter your name

Name: @3
Please enter your name

Name: John

Similarly to ensure that the user has entered alphabets and/or numbers we can use the isalnum() function. This is useful in prompting user password. Following is an example python code.

while True:
    pw = input('Password: ')
    if pw.isalnum():
        break
    print('Please use alphabets and numbers')

The result of running this are as follows.

Password: hdulsen



Password: @#$
Please use alphabets and numbers

Password: 123

So in this way you can prompt user to input integers, alphabets or both and check that they entered the correct data types.

See some books like Learning Python: Powerful Object-Oriented Programming.

No Comment to " How to check user input data types in python? "