How to Check User Input is a Number or String in Python?

The input data can be of various data types such as integer, float, string, etc. While working on larger programs, each input needs to be checked before processing for a function. For instance, the string cannot be processed in a function that supports only integers. 

Keeping this in view, this post will address the possible methods to check whether user input is a number or string in Python.

Method 1: Using isdigit() Function

The “isdigit()” function is used to check whether the user input is digits or not. The below code uses the “isdigit()” function to check user input:

Code:

num1 = input("Enter Value: ")
if num1.isdigit():
    print("Input Value is Number")
else:
    print("Input Value is String")
  • The “input()” function is used to take the input from the user.
  • The “if” statement is used along with the “isdigit()” function to check whether the given value is a number or string.
  • The “else” will execute the print statement when the input value is a string or not equal to the digit/number.

Output:

The input value is a number.

Method 2: Using float() and isinstance() Function

The “float()” function is used along with the “isinstance()” function to check whether the user input is a number or string:

Code:

value = input('Enter a Value: ')
try:
    data = float(value)
    print('Input Value is Number')
except:
    if isinstance(value, (str)):
        print('Input Value is String')
  • The “input()” function is used to input the user input and this value will be stored in a variable named “value”.
  • In the “try” block, the “float()” function is utilized. The “float()” function accepts the user input as an argument and converts it into a floating value.
  • If the input is not a number, then the block named ‘except” is executed.
  • In the “else” block, the “isinstance()” function is used to check whether the given value is a string or not. If the value is a string, the “print()” function is executed.

Output:

The input value is a number.

The input value is a string.

Method 3: Using isnumeric() Function

The “isnumeric()” function is used to retrieve the boolean value if the input is a number. Here is an example:

Code:

value = input("Enter Value: ")
if value.isnumeric():
  print("Input value is Integer!")
else:
  print("Input value is Not an Integer!")
  • The “input()” function takes the user input.
  • The “isnumeric()” function is used along with the “if-else” statement to check whether the input is a number or string.

Output:

The input value is an integer/number.

Method 4: Using isinstance() Function to Check Specified Input

The “isinstance()” function is used to check the specified type of the input object. The “isinstance()” function is used in the below code to check user input value:

Code:

num = 180445
print('Input Value is Number: ',isinstance(num, (int, float)))
print('\nInput Value is String: ',isinstance(num, (str)))
  • The integer is initialized in a variable named “num”.
  • The “isinstance()” function accepts the input integer as a first argument and multiple data type as a second argument.
  • This function retrieves a boolean value by checking whether the specified input value is of an int, float, or string type.

Output:

Based on the output above, the input value is a number.

Conclusion

The isdigit(), isinstance(), float() with isinstance() and isnumeric() functions are used to check whether the user input is a number or string in Python. The “isdigit()” function is used to check whether the input number is a digit or not. Similarly, the isinstance() function individually or with the float() function is used to check whether the input value is a number or string. 

This guide provided various ways to check the user input number or string value.