Python – Check If String is an Integer or Float

Do you want to verify if a given String in Python is Integer Value or Float? This article will show you how to check if the string is an integer or float in Python.

Suppose you want to write a program that takes input as a string value in Python and then you want to check if the input is a number and then you want to perform your further logic and if the string is not a number, you want to throw an error.

In this case, we can use any of the below-mentioned methods, such as using the String class’s isdigit() method.

Using isdigit() Method of String Class

You can use the isdigit method of the String class to check if the given string input is a number or not. Suppose if the given input is not a number in that case this method will return false and hence based on the return of this method you can write your further logic in the program.

Let us see in the below example code the use of the isdigit() method in Python. Note that this method only verifies if the input is Integer and it throws an error message when Float Value is Input.

#Taking a input from the User as Number
getInputString = input("Enter the Number.\n")

#If the Input String is Number Print the Number
if getInputString.isdigit():
  print("You entered the Number: {}".format(getInputString))
#Else print an Error message
else:
  print("Error: Input is not a Number")

Output:

#Example 1
Enter the Number.
5
You entered the Number: 5


#Example 2
Enter the Number.
coduber
Error: Input is not a Number

#Example 3
Enter the Number.
10.12
Error: Input is not a Number

As you can see in the above code float is not recognized using the isdigit method. This is because the isdigit method is not recognizing decimals as digits.

To overcome this problem we need to modify your above code and use the try-catch method to check if the value can be parsed as float or integer or none.

Using isdecimal() Method of String Class to Check If String is Float or Integer

In the String class of Python, we have a function or method named isdecimal this method can be used to check if the given string is a float value or not. If the input string is a float then this method will return a True and if it is not a float value then this method will return False.

Let us see in the below example code to understand the usage of isdecimal() method.

#Taking a input from the User as Number
stringNumber = input("Enter the Float Value.\n")

#If the Input String is Number Print the Number
if stringNumber.isdecimal():
      print("{} is Float".format(stringNumber))
else:
      print("Error: Not a Float Value.")

Output:

Example 1:
Enter the Float Value.
10.23
10.23 is Float

Example 2:
Enter the Float Value.
coduber
Error: Not a Float Value.

As you can see in the above code You can easily check the input string is float or not. Similarly, you can check if the number is an integer or not once you have verified that the number is a decimal using the above method.

Let me expand the above code for checking the string for integer as well.

#Taking a input from the User as Number
stringNumber = input("Enter the Number.\n")

#If the Input String is Number Print the Number
if stringNumber.isdecimal():
      if stringNumber.isdigit():
            print("{} is Integer.".format(stringNumber))
      else:
            print("{} is Float".format(stringNumber))
else:
      print("Error: Not a Float Value.")

Output:

Example 1:
Enter the Number.
10.5
10.5 is Float

Example 2:
Enter the Number.
coduber
Error: Not a Float Value.

Example 3:
Enter the Number.
20
20 is Integer.

Using Try and Except To Check If String is Float or Integer In Python

Using Try and Catch method is very intuitive as even in the above problem we do not want to continue if the error is hit. Hence to overcome the above problem you can use the Try and except method in Python to handle the error.

Let us see in the below example Code. We are also using the is_integer method to check if the given number is Integer only. This method returns true if the float is only Integer else this method will return false if the number is Float Value.

#Taking a input from the User as Number
getInputString = input("Enter the Number.\n")

#If the Input String is Number Print the Number
try:
  #Check if the Number is Float if it pass then you can easily 
  #Convert it to Integer
  newFloatNumber = float(getInputString)
  if newFloatNumber.is_integer():
        print("The Integer Value you Entered is: {}".format(int(newFloatNumber)))
  else:
        print("The Float Value you Have Entered is: {}".format(newFloatNumber))
except ValueError:
  print("Error: Input is String and not Integer or Float")

Output:

#Example 1:
Enter the Number.
coduber 
Error: Input is String and not Integer or Float

#Example 2:
Enter the Number.
10
The Integer Value you Entered is: 10

#Example 3:

Enter the Number.
10.2
The Float Value you Have Entered is: 10.2
Python - Check If String is an Integer or Float

Wrap Up

As you saw in the above code we handled all the aspects of float, Integer in Python. Now using the method you can easily find or check if the given string is a float or an integer.

We have used method isdigit(), is_integer(), and try-except method to achieve our solution to the above problem.

I think I have provided the most efficient method that you can use in your code. Let me know in the comment section if you have any better method from what is mentioned above then I will be happy to share it on my page.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Python – Convert Bytes To String Quickly
  2. Python Print 2 Decimal Place Quickly
  3. How To Add Numbers Using For Loop in Python
  4. How Can I Add New Keys To Dictionary Python
  5. Finding The Index Of An Item In A List Python

Leave a Comment