In this lesson, you will learn how to check user input is a number or string in Python. We will also cover how to accept numbers as input from the user. When we say a number, it means it can be integer or float.
Understand user input
Python 3 has a built-in function input() to accept user input. But it doesn’t evaluate the data received from the input() function, i.e., The input() function always converts the user input into a string and then returns it to the calling program.

Let us understand this with an example.
Output Enter number and hit enter 10 Printing type of input value type of number class 'str'
As you can see, The output shows the type of a variable as a string (str).
Solution: In such a situation, We need to convert user input explicitly to integer and float to check if it’s a number. If the input string is a number, It will get converted to int or float without exception.
Convert string input to int or float to check if it is a number
How to check if the input is a number or string in Python
- Accept input from a user
Use the
input()function to accept input from a user - Convert input to integer number
To check if the input string is an integer number, convert the user input to the integer type using the
int()constructor. - Convert input to float number
To check if the input is a float number, convert the user input to the float type using the
float()constructor. - Validate the result
If an input is an integer or float number, it can successfully get converted to
intorfloattype. Else, we can conclude it is a string
Note: If an input is an integer or float number, it can successfully get converted to int or float, and you can conclude that entered input is a number. Otherwise, You get a valueError exception, which means the entered user input is a string.
Program :
Output Enter your Age 28 Input is an integer number. Number = 28 Enter any number 3.14 Input is a float number. Number = 3.14 Enter the last number 28Jessa No.. input is not a number. It's a string
- As you can see in the above output, the user has entered 28, and it gets converted into the integer type without exception.
- Also, when the user entered 3.14, and it gets converted into the float type without exception.
- But when the user entered a number with some character in it (28Jessa), Python raised a
ValueErrorexception because it is not int.
Use string isdigit() method to check user input is number or string
Note: The isdigit() function will work only for positive integer numbers. i.e., if you pass any float number, it will not work. So, It is better to use the first approach.
Let’s execute the program to validate this.
Output Enter number and hit enter 45 User input is Number Enter number and hit enter 45Jessa User input is string
Also, If you can check whether the Python variable is a number or string, use the isinstance() function.
Example
Only accept a number as input
Let’s write a simple program in Python to accept only numbers input from the user. The program will stop only when the user enters the number input.
Output Please enter a number 28Jessa This is not a number. Please enter a valid number Please enter a number 28 Input is an integer number. Input number is: 28
Practice Problem: Check user input is a positive number or negative
Show Solution
Next Steps
Let me know your comments and feedback in the section below.
Also, Solve:

input1 = input(“Enter your Age “)
check_user_input(input1)
input2 = input(“Enter any number “)
check_user_input(input2)
input2 = input(“Enter the last number “)
check_user_input(input2)
I appreciated your effort for this excellent website, and thanks allot.
I think about last inputs you mean(input3)
input3= input(“Enter the last number “)
check_user_input(input3)
in the
check_user_inputfunction, if I enter any float value, then why it is not converted into an integer when “int(input)” is used………………….please explain
I am new to python, so I am confused because I learned that if int/float/str is used, it will convert the value into integer/float/string if possible.
In the case of float value, it can be converted to an integer, but in this case, it is not converted into an integer …………
so I am really confused; please help
Create a python program to input any number through the keyboard and
display (print)the number. how to create this# this program is not executing , can anybody correct this one
Hi, Shiva. I’m no expert and am also learning. But I can see the following problem: any value can be a string. If you type “123” in an input(), it will not consider it as a string.
Now, if you try something like:
try:number=int(input("enter the data: " ))
print("data contains an integer")
except ValueError:
print("data does not contain an integer")
print("program ends")
You may get the expected error. Note the appropriate typing of “ValueError”, not “valueerror” (Python diferentiates that). Try using a IDE, such as VSCode, to get fast feedback for small typing errors like that.
VERY nice. Thanks!
Can you please suggest a youtube video on how to make a python game????
How about this what is the input and output. can you help me? I just want to know how?
enter your age 60
input is an integer number, number = 60
enter any number Philippines100
No.. input is not a number. it’s a string
enter the last number 52
input is an integer number. number = 52
Hello, I’m working on a particular project in python and I tried looking at this lesson, but I can’t seem to find a way to apply it to my situation. I thought maybe you might know?
Program’s current objective: I need to be able to ask the user to input ‘Integer’ values for the efficiency of the vehicles rating. I also want the program to not crash if the user puts an invalid value. i.e. floating point or string value. Although I can’t seem to be able to get the program to re-ask the question until it achieves a valid answer. Then will carry on the operations from there.
Please check this code:
I think this should solve your problem.
Thank you very much this saved my game project 🙂
You are welcome.
Shouldn’t there be a:
except ValueError: try: val = float(num) print("Input is an float number.") print("Input number is: ", val)yes, propably a miss
Thank you so much. Great help!
You’re welcome
#Write a Python program to check a string represent an integer or not.
s =input('enter a value : ') print(type(s)) if type(s)==str: print(s,"The string is an integer") else: print( s,"The string is not an integer")why my code print always the two massage
It will always print two messages. first is
print(type(s))and second is from the the if-else condition.You typed wrong messages in the if-else block.
In my code, I want to enter a string. If the default type for input is a string, why do I have to put quotes around it?
python mscode.py
enter series of words separated by spaces: aa bb Traceback (most recent call last): File "mscode.py", line 17, in main text = input("enter series of words separated by spaces: ") File "", line 1, in NameError: name 'a' is not definedpython mscode.py
enter a series of words separated by spaces: “aa bb”
This works properly. Why do I need quotes?
Hi Edward, You don’t need to provide it in double-quotes. Can you please paste your code so I can look into it
next = input(“>”) if "10" in next or "11" in next or "99" in next: how_much=int(next) else: dead(“Man, learn to type a number”) exit(0) if how_much < 50: #here you didnt have brackets print("Nice, you're not greedy, you win!") exit(0) else: dead("You greedy bastard!")#and maybe you have as well indentation errors
You were making it complicated, try this, this is simple. And yes, your code has too many errors like variable definition and using functions.
I am trying this program on python 2.7.1:
next = raw_input(">") try: if (next.isdigit()): how_much=int(next) else: dead("Man, learn to type a number") exit(0) if how_much < 50: print "Nice, you're not greedy, you win!" exit(0) else: dead("You greedy bastard!")I get the message: if how_much < 50: ^ SyntaxError: invalid syntaxCan you explain to me why this happens and how I can solve it
what if you only want letter input and no int or float?
Hey, Abdul use input() method without doing any explicit casing. input() method always converts user input into a string. a single char is also nothing but the string in Python.