What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Check if the String is Integer in Python

  • Nov 28, 2023
  • 7 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Mradula Mittal
How to Check if the String is Integer in Python

The digital world of today is shifting toward data science and machine learning. "DATA" is the key point and the centroid for the upcoming technology. This data can take several forms, but it is typically in the form of values (numbers). There are numerous methods for gathering data but the most common way is using Strings.  In this article, you will learn how to check if a String is an integer in Python. Before moving on, let's take a quick recap of Integers and Strings in Python.

What is an Integer?

An Integer is a whole number, which includes negative numbers, positive numbers, and zero. Integers don't include any fractions or rational parts.

For example: 23, -39, 4 -are integers & 3.54, 6.34  are not integers.

What is a String in Python?

Strings are sequences of characters, enclosed between quotations, double-quotes (" "), or single-quotes (' '). These characters include alphabets (a-z) (A-Z), numbers (0-9), and special characters ( -, _ , %, $, & and more). In Python, any value enclosed between the quotation marks is termed as strings. 

For example: "Favtutor" , '54', "ab1" , "sq_rt"

Why do we need to check for Integers in a String?

There are many instances where you would be required to check if a string is an integer or if a string contains any number. To be precise, you have to look out for cases where strings ("45") are actually numbers (45). 

 Let's take a few examples from our daily life, where you need to check for integers in a string:

  1. Validating user input
  2. Looking for data within strings
  3. Checking for valid input details in a field (Consider a form and a field for name)

How to Check If String is an Integer in Python?

Python offers many methods that check if a string has any number in it. Some of them are listed below:

Method 01) Using Error Handling with int()

This method is strictly to check for "integers" in Python. Using the exception-handling approach, we determine whether the "string" is an "integer" or not. This entails "trying" to typecast the string into an integer. If it does not throw any ValueErrors, it is an integer. On the contrary, if an error occurs, the except block is executed, which changes the flag to False.

Let's see an example:

# initalising strings

      from multiprocessing.sharedctypes import Value

      

      

   str1 = "10"

   str2 = "FavTutor blogs"

   str3 = "for10"

   # creating a list of all the variables to check multiple cases

   variables = [str1, str2, str3]

      

   # declaring a flag for check

   flag = True

      

   for var in variables:

   # Applying error - handling method

          try:

              # try converting to integer

              int(var)

          except ValueError:

              flag = False

      

          # flag check

          if flag:

              print("This is an integer")

          else:

              print("This is not an integer")

 

Note that I've initialized the flag to True, since we are changing it to False, in case of ValueError.

Output:

This is an integer

This is not an integer

This is not an integer

    

 

We may conclude from the output that the exception-handling approach only works for integers as strings and not for strings comprising both integers and strings (or other objects). Before we proceed, let's apply this procedure to a variety of test cases:

# initializing variables

   

   # negative number

   case1 = "-10"

   

   # decimal number

   case2 = "49.90"

   

   # an integer

   case3 = 55

   

   # creating a list of all the variables to check multiple cases

   cases = [case1, case2, case3]

   

   # declaring a flag for check

   flag = True

   

   for case in cases:

       # Applying error - handling method

       try:

           # try converting to integer

           int(case)

       except ValueError:

           flag = False

   

       # flag check

       if flag:

           print(case, " is an integer")

       else:

           print(case, " is not an integer")

   

 

This displays that this method works in identifying both, positive and negative integers, in a string. Though, when applied to an integer itself, it produces a False value. Do you know why?

Output:

-10  is an integer

49.90  is not an integer

55  is not an integer

    

 

Since we have tried and tested this method against all possible test cases, let's list out the cases and whether this method works for them or not.

Input Type Input example Result obtained
Positive Integer as String "10" An integer
Negative Integer as String "-10" An integer
Decimal number as String "4.5" Not an Integer
A String "FavTutor" Not an Integer
An Integer and a String "10 Methods" Not an Integer
An Integer as Integer 10 Not an Integer

 

The above table displays the output of the exception-handling method in all possible test cases. 

Method 02) Using isdigit()

The isdigit() method is an attribute of the string object to determine whether the string is a digit or not. This is the most known method to check if a string is an integer. This method doesn't take any parameter, instead, it returns True if the string is a number (integer) and False if it's not.

The code below displays the use of isdigit() method on all the potential test cases I could come up with. You are free to try the code on other test cases.

# initializing variables

   

   # positive integer as string

   case1 = "10"

   

   # negative integer as string

   case2 = "-10"

   

   # float as string

   case3 = "19.90"

   

   # a string

   case4 = "Favtutor article"

   

   # an integer and a string

   case5 = "10 Methods"

   

   # an integer

   case6 = 10

   

   # creating a list of all testcases

   testcases = [case1, case2, case3, case4, case5, case6]

   

   # creating for loop for the testcases list

   for case in testcases:

       # calling the isdigit() method

       if case.isdigit():

           print(case, " is an integer.")

       else:

           print(case, " is not an integer.")

   

 

We have applied the isdigit() method to all the test cases as discussed above. You can observe the difference between the exception-handling method and isdigit() method from the output below:

     10  is an integer.

    -10  is not an integer.

    19.90  is not an integer.

    Favtutor article  is not an integer.

    10 Methods  is not an integer.

    Traceback (most recent call last):

      File ".\temp.py", line 27, in <module>

        if case.isdigit():

    AttributeError: 'int' object has no attribute 'isdigit'

    

 

Did you notice? The above code only worked for positive integers as a string. Unlike the exception-handling method, the isdigit() method doesn't work for negative integers. Also, it gave an error when called on an integer (integer object). Why so? Remember that isdigit() is an attribute for the string objects and not integers!

In order to check for negative numbers using isdigit(), you'll need to perform an additional check before checking for isdigit(). You can try:

# initializing variables

   

   # positive integer as string

   case1 = "+10"

   

   # negative integer as string

   case2 = "-10"

   

   case3 = "10"

   

   # creating a list of all testcases

   testcases = [case1, case2, case3]

   

   # creating for loop for the testcases list

   for case in testcases:

       # check for ['+' and '-'] sign

       if case[0] in ["+", "-"]:

           # calling the isdigit() method

           if case[1:].isdigit():

               print(case, " is an integer.")

           else:

               print(case, " is not an integer.")

       else:

           # an additional check for unsigned integers

           if case.isdigit():

               print(case, " is an integer.")

           else:

               print(case, " is not an integer.")

   

 

Adding an additional check for symbols ( '+' and '-") ensures that when encountering a negative number, depicted by the '-' sign, the code doesn't avoid the number and acknowledges it as an integer. Also, I've added another check in the else block. So that even when the string doesn't have any sign as a prefix, it goes through isdigit() method.

Output: 

+10  is an integer.

-10  is an integer.

10  is an integer.

    

 

As we did for the exception-handling method, let's create a quick table for the cases where isdigit() method successfully works to check if a string is an integer in Python-

Input type Input example Result produced
Positive Integer as string "10" An integer
Negative Integer as string "-10" Not an integer. (Requires an additional check for signed numbers)
Float number as String "19.90" Not an Integer
String "FavTutor" Not an Integer
An Integer + String "10 methods" Not an Integer

 

Method 03) Using isnumeric() method

Python offers isnumeric() method that checks whether a string is an integer or not. This method is similar to the isdigit() method but with a few differences. The isnumeric() method checks whether all the characters in the string are numeric. While the isdigit() method checks whether the strings contain only digits. Also, isnumeric() method works for Unicode. It checks whether a character is a Unicode representation of a numeric value. 

Now, you must be wondering about "Unicode"?

You'll be surprised that Python strings use Unicode Standards to represent characters.

What is Unicode? Unicode is a specification with the objective of listing every character used by human languages. Well, there's a long story behind the introduction of the Unicode Standard. But cutting through the chase, Unicode Standards were introduced so as to provide codes (computer-interpreted code like ASCII) for all the languages in the world. It gives each character its own unique code. Hence, making Python accepts formats in languages other than English, like Roman numerals. 

Coming back to the isnumeric() method! Let's take an example - 

# initializing variables

   

   # an integer

   case1 = "+10"

   

   # negative integer as string

   case2 = "-10"

   

   case3 = "10"

   

   # a string

   case4 = "Favtutor article"

   

   # an integer and a string

   case5 = "10 Methods"

   

   # an integer

   case6 = "10.6"

   

   # creating a list of all testcases

   testcases = [case1, case2, case3, case4, case5, case6]

   

   # creating for loop for the testcases list

   for case in testcases:

       if case.isnumeric():

           print(case, " is an integer.")

       else:

           print(case, " is not an integer.")

   

 

Output:

+10  is not an integer.

    -10  is not an integer.

    10  is an integer.

    Favtutor article  is not an integer.

    

    10 Methods  is not an integer.      

    10.6  is not an integer.

    

 

You can apply the same condition, as we did before, to check for the signs ('+' and '-'). Hence, we can also detect negative numbers with isnumeric() method.

Method 04) Using re module

Python offers an in-built "re module" to work with Regular expressions. Actually, 're' stands for "Regular Expression".

What are "Regular Expressions"? Regular Expressions, or RegEx, are a series of characters that form a search pattern. These expressions are used to find a certain pattern within a string.
Many functions in Python's re-module can be used to determine whether a string is an integer. To be more specific, they "search" within the given string format for the defined pattern (in our case, digits). This specific pattern can be expressed by "metacharacters" or "special sequences." Before moving on to the method, let's learn about metacharacters and special sequences.

What are Metacharacters and Special Sequences? Metacharacters are characters with special meanings. They are commonly used in regular expressions to represent a set of characters or to mark the beginning, end, or occurrences of a string. 

For example - " [] " represent a set of characters; " ^ " marks the beginning of a string and " \ " marks the beginning of a special sequence.

You must know about the set " [] " character in order to check if a string is an integer in Python using the re-module. The " [0-9] " returns a match for any digit between 0 and 9. (which we are looking for!)

A Special Sequence is also a group of characters with special meanings. The only difference is that it starts with " \ ". So, "\" followed by any character makes that sequence a special sequence.

 For example - " \d " returns a match where the string contains digits (0-9); " \D " returns a match where the string does not contain digits and many more.

Let's get back to using the RegEx functions to check if a string is an integer-

Function 1: Using re.search()

This method returns a Match object (an object containing information about the search and result) if there's a match for the string. It searches for a match of the specified pattern (here 0-9) in the given string. Hence, it doesn't strictly check if a string is only an integer. Rather it checks whether the string contains any digits. 

Let's take a look at an example before trying out the search() method with all our test cases:

# import the re module

    import re

    

    case = "10"

    # calling search method from re module

    # using [0-9] set metacharacter for digits (0 to 9)

    digits = re.search("[0-9]", case)

    # Printing the found result

    print('Found digits at indexes: ', digits)

    

 

It is important to import the re-module in order to call for the search() function. The re.search() function searches for digits [0-9] among the given string and returns a Match Object, as seen below-

Found digits at indexes:  <re.Match object; span=(0, 1), match='1'>

 

The span attribute displays the start and end positions of the match. Note that the search() function stops as soon as it gets its first match. Also, it returns None in case no match is found.

Let's try the search() function on all of our test cases:

# import the RegEx module

   import re

   

   # initializing variables

   # a positive integer

   case1 = "+10"

   

   # negative integer as string

   case2 = "-10"

   

   case3 = "10"

   

   # a string

   case4 = "Favtutor article"

   

   # an integer and a string

   case5 = "10 Methods"

   

   # an integer

   case6 = "10.6"

   

   # creating a list of all testcases

   testcases = [case1, case2, case3, case4, case5, case6]

   

   # creating for loop for the testcases list

   for case in testcases:

       # calling the re.search() method

       # Using [0-9] set metacharacter to check for digits between 0-9

       if re.search("[0-9]", case):

           print(case, " has a number.")

       else:

           print(case, " don't have any number.")

   

 

Output:

+10  has a number.

    -10  has a number.

    10  has a number.

    Favtutor article  don't have any number.

    10 Methods  has a number.

    10.6  has a number.

    

 

Surprised by the Output? Instead of looking at whether the string is an integer, this method actually outputs whether a string contains any number, be it an integer or float or a mix of integers and strings. 

Replace the [0-9] with "\d" and you will see your desired output-

# import the re module

import re




case = "10 methods"

# calling search method from re module

# Replacing [0-9] with "[+-]?\d+$"

digits = re.search("[+-]?\d+$", case)

# Printing the found result

print("Found digits at indexes: ", digits)

 

Although this replacement also works only to distinguish the numbers from strings and not specifically for integers.

Output:

Found digits at indexes:  None

 

Function 2: Using re.findall() 

The findall() function returns a list of all the digits it "finds" in the string. Unlike the search() function in the re module, it parses through the complete string and returns a list of all the digits (matching through the pattern [0-9]) in the string. Let's take a quick example of how it works: 

# import the re module

    import re

    

    case = "106"

    # calling findall() function from the re module

    # using [0-9] set metacharacter for digits (0 to 9)

    digits = re.findall("[0-9]", case)

    # Printing the found result

    print('Found digits at indexes: ', digits)

    

 

Output:

Found digits at indexes:  ['1', '0', '6']

 

Note how the findall() function has listed out all the digits in the string. Let's use the findall() function with all our test cases-

# creating for loop for the testcases list  

    for case in testcases:

        # calling the re.search() method

        # Using [0-9] set metacharacter to check for digits between 0-9

        temp = re.findall("[0-9]", case)

        if temp :

            print(case, " has digits: ", temp)

        else:

            print(case, " don't have digits: ", temp)

    

 

You can look for the testcases list from the above example. The findall() function returns an empty string in case it doesn't find any match for the pattern in the string.

Output:

+10  has digits:  ['1', '0']

-10  has digits:  ['1', '0']

10  has digits:  ['1', '0']

Favtutor article  don't have digits:  []

10 Methods  has digits:  ['1', '0']

10.6  has digits:  ['1', '0', '6']

 

This function() also doesn't work only for integers, but to list out (or check) if a string has any digit (0-9).

Function 3: Using re.match()

This function also returns a Match Object when it finds a match for the specific string ([0-9] or "[+-]?\d+$" ) in the given string. For instance, 

# import the re module

import re




case = "-10"

# calling search method from re module

# using [0-9] set metacharacter for digits (0 to 9)

digits = re.match("[+-]?\d+$", case)

# Printing the found result

print("Found digits: ", digits)

 

Output:

Found digits:  <re.Match object; span=(0, 3), match='-10'>

 

Carefully observe the output. You'll notice the difference between the search() and match() functions. 

Hard to find? Let me help you! 

You know that both, match() and search() functions, return Match Objects for the strings. The search() function returns as soon as it gets one of the digits while the match() function completes the "match" and returns the span (start-end index) for all the digits in the string.

I'll leave the task of applying the match() function to you! Try out this function on different test cases and compare the outputs. You'll have a better idea of how this module works.

If you are still confused about any of the above methods, we can connect you with a qualified python tutor online anytime to solve your queries.

Conclusion

Determining whether a string is an Integer in Python is a basic task carried out for user input validation. But there's a difference between the task "Check if a String 'is' an Integer in Python" and "Check if a String 'has' any number in Python". In the above article, the isdigit(), isnumeric(), and exception-handling methods can be used specifically for the first, i.e. "To check if a string is an integer" whereas the re-module is used for the latter.

There are also methods like using any() and map() functions with isdigit() (or other specified methods). You can also find or even create different variations with the methods stated above. Each method has its own application area. Remember to compare different methods to find out the best one for your code. Happy Coding!

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Mradula Mittal
I'm Mradula Mittal, a tech community enthusiast with a knack for content writing. Always eager to learn more about Python and Machine Learning. Having an analytical brain, I'm particular about my research and writing. I live by the motto "the more you share, the more you learn" and being a techie, it's fun to learn, create and share.