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:
- Validating user input
- Looking for data within strings
- 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:
Note that I've initialized the flag to True, since we are changing it to False, in case of ValueError.
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:
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?
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.
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:
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:
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.
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 -
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:
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-
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:
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-
Although this replacement also works only to distinguish the numbers from strings and not specifically for integers.
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:
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-
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.
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,
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.
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!