Python

Python Check if Character is Number

You may need to check if a Python string contains a number at some point in your application. Verifying each character in a Python string using the string isdigit() function is a straightforward way to see if it includes a number. After that, we will obtain a list of Booleans, and if any of them are True, the string contains at least one integer. There are several approaches to resolving this problem, and this article will discuss a few of them.

Example 1:

The first example uses the isnumeric() function to determine whether a given or input string is an integer or not. This method is one of the best and most commonly used ways to check whether or not a string is an integer. This method is pre-installed in Python. It returns True if the characters are numeric; otherwise, False. It is vital to notice that the isnumeric() function tests if all of the characters in the string are numeric rather than if the string represents an integer. If you want to prevent these situations, choose a different strategy. After that, let’s have a look at the following code. We declared a string ‘a’ with the value ‘9442422’, as you can see in the first line of code. The isnumeric() method is used to verify whether or not ‘9442422’ is an integer. In this case, it returned ‘True’ because it is an integer.

The second illustration is identical to the first one in which we have checked the integers. The difference is that we have combined integer and string values. In this scenario, the isnumeric() function will give False. After that, we combined the if-else and isnumeric() routines to add some extra phases. Here, we declared and set the value of our variables to ‘4540’. After that, we used flow control instructions as well as the isnumeric() function to see if the given string was an integer. In this case, it is an integer. We will obtain an Integer output as a result. If the value comprises anything other than integers, the outcome will be the same as shown in the code.

a = '9442422'
print(a.isnumeric())
b ='code15'
print(b.isnumeric())
c = '4540'
if c.isnumeric():
   print('Integer')
else:
print('Not an integer')

Here is the output.

Example 2:

We can also determine if the string is an integer or not by using Python’s exception handling method. Here is a quick review of how Python handles exceptions if you are new. The try statement of Python can be used for this purpose which is to manage exceptions. The important method that could result in exclusion is contained in the try clause. The code that handles exceptions is placed in the except clause.

As a consequence, after the exclusion has been detected, we can choose which procedures to undertake. Please go through the example program (mentioned below) to understand how it works. We have made a string called ‘new_str’ with the value ‘123ab.’ The value of string ‘new_str’ looks to be an integer at first glance, but it is not. As an outcome, it was declared incorrect. After that, we have converted a string into an integer with the int() function. If the string has non-numeric characters, this method will throw a ValueError. It indicates that the string is not an integer.

new_str = '123ab'
isInt = True
try:
   int(new_str)
except ValueError:
isInt = False
if isInt:
   print('Integer')
else:
print('Value is not an integer')

Here you can view the result.

Example 3:

The isdigit() method is demonstrated in this example. In Python, we may use the isdigit() function to see if a string is an integer or not. The isdigit() procedure will give True if the characters in a string are digits. Refer to the below example for more guidance. We have put the input of a string into the variable ‘a.’ After that; we have used control commands as well as the isdigit() function to see if the input is an integer or not.

a = input("Enter value: ")
if a.isdigit():
print("Integer ")
else:
print("String ")

Below is the output.

Example 4:

In Python, we may use the any() and map() functions to see if a string is an integer or not. In Python, the any() method accepts an iterable. The iterable object is either a series, a collection, or an iterator. You can enter as many iterables as you wish. All you have to do is make sure each iterable has its own method argument. This method will return true if the element(s) in the iterable are integers; otherwise, this function will be false. The map() method will produce a list of results once you’ve executed the function for each item in an iterable. We have taken input as a string in the example above, which is ‘abab.’ Then, we use Python’s any(), map(), and isdigit() functions to see if the string is an integer.

a= "abab"
check= any(map(str.isdigit, a))
print(check)

Because the input string is ‘abab,’ we receive False, as shown in the next screenshot.

Conclusion:

So, if you have made it this far, it means that you now understand all of the many ways to check if a string is an integer in Python. We have discussed many methods, including the isnumeric(), isdigit(), exception handling mechanism, any(), and map() function with detailed examples and explanations.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content