Python Check User input is a Number or String | 您所在的位置:网站首页 › inputfunction › Python Check User input is a Number or String |
Check user Input is a Number or String in Python
Updated on:聽April 24, 2021 | 27 Comments 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 inputPython 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. number1 = input("Enter number and hit enter ") print("Printing type of input value") print("type of number ", type(number1))Code language: Python (python) 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 numberHow to check if the input is a number or string in Python Accept input from a userUse the input() function to accept input from a user Convert input to integer numberTo 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 numberTo check if the input is a float number, convert the user input to the float type using the float() constructor. Validate the resultIf an input is an integer or float number, it can successfully get converted to int or float type. 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 : def check_user_input(input): try: # Convert it into integer val = int(input) print("Input is an integer number. Number = ", val) except ValueError: try: # Convert it into float val = float(input) print("Input is a float number. Number = ", val) except ValueError: print("No.. input is not a number. It's a string") 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)Code language: Python (python) 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 ValueError exception because it is not int. Use string isdigit() method to check user input is number or stringNote: 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. def check_is_digit(input_str): if input_str.strip().isdigit(): print("User input is Number") else: print("User input is string") num1 = input("Enter number and hit enter") check_is_digit(num1) num2 = input("Enter number and hit enter") check_is_digit(num2) Code language: Python (python) Output Enter number and hit enter 45 User input is Number Enter number and hit enter 45Jessa User input is stringAlso, If you can check whether the Python variable is a number or string, use the isinstance() function. Example num = 25.75 print(isinstance(num, (int, float))) # Output True num = '28Jessa' print(isinstance(num, (int, float))) # Output FalseCode language: Python (python) Only accept a number as inputLet’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. while True: num = input("Please enter a number ") try: val = int(num) print("Input is an integer number.") print("Input number is: ", val) break; except ValueError: try: float(num) print("Input is an float number.") print("Input number is: ", val) break; except ValueError: print("This is not a number. Please enter a valid number") Code language: Python (python) 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 Solutionuser_number = input("Enter your number ") print("\n") try: val = int(user_number) if val > 0: print("User number is positive ") else: print("User number is negative ") except ValueError: print("No.. input string is not a number. It's a string")Code language: Python (python) Next StepsLet me know your comments and feedback in the section below. Also, Solve: Python input and output exercisePython input and output quizPython exercise for beginnersPython Quiz for beginnersFiled Under: Python, Python Basics |
CopyRight 2018-2019 实验室设备网 版权所有 |