Understanding isnumeric() Method in Python
This blog will make you understand isnumeric() Method in Python. We have covered multiple examples here to explain you Python’s isnumeric() concept better. Let’s explore!
To determine if a string consists entirely of numerical characters (including numbers 0-9, Unicode numeric values, and exponents), the isnumeric() method in Python is used.
In this article, we will discuss this function and understand its use with the help of examples. We will be covering the following sections:
Explore free Python courses
Introduction to isnumeric() Method in Python
The isnumeric() method is a built-in function in Python that can be used to check whether a given string consists entirely of numeric characters. The method returns True if all characters in the string are numeric, including Unicode numeric characters and special characters such as exponents. Otherwise, it returns False.
This method can be useful in various situations, such as verifying user input for a numerical value or checking if a string can be safely converted to an integer or float. It can also be used in data processing tasks where numeric values must be identified and extracted from a larger string.
Must Check: Python Online Course and Certifications
Syntax of isnumeric()
The syntax of isnumeric() method in Python is given as:
string.isnumeric()
Suggested read: Types of Functions in Python
Parameters of isnumeric()
The isnumeric() function does not take any parameters.
Return Value of isnumeric()
The isnumeric() method method returns a boolean value – True if the string contains only numeric characters, and False if the string contains non-numeric characters like alphabets.
Exceptions of isnumeric()
If we use the correct syntax, the isnumeric() method in Python will not generate an error. However, if a string passed to the method contains white spaces (or empty spaces), the method will return False even if the string is a numeric value.
Best-suited Python courses for you
Learn Python with these high-rated online courses
Examples of Using the isnumeric() Method in Python
Example 1: Validating user input
When accepting user input, it’s important to ensure that the input is of the correct type. The isnumeric() method checks whether a user has entered a numeric value. For example, if a program expects the user to input an integer, we can use isnumeric() to validate the input:
user_input = input("Please enter an integer: ")
if user_input.isnumeric(): num = int(user_input) print("The input is a valid integer.") else: print("The input is not a valid integer.")
Output:
Please enter an integer: 32
The input is a valid integer.
In this example, we ask the user to enter an integer, and then use isnumeric() to check if the input is numeric. If the input is numeric, we convert it to an integer and proceed with the program. If the input is not numeric, we display an error message.
Example 2: Parsing data from a file
Sometimes, we need to extract numeric data from a larger file. For example, we may have a text file that contains a mix of alphabetic and numeric data, and we want to extract only the numeric data. We can use isnumeric() to accomplish this:
with open('data.txt', 'r') as f: for line in f: for word in line.split(): if word.isnumeric(): num = int(word) # Do something with the numeric data
In this example, we open a text file and loop through each line. For each line, we split the line into individual words, and then use isnumeric() to check if each word is numeric. If a word is numeric, we convert it to an integer and proceed with processing.
Example 3: Converting string to integer
Int() can safely convert a string into an integer by using the isnumeric() method.
For example:
num_str = "123"
if num_str.isnumeric(): num = int(num_str) print("The integer value is:", num) else: print("The string cannot be converted to an integer.")
Output:
The integer value is: 123
In this example, we first use isnumeric() to check if the string can safely convert to an integer. If the string is numeric, we convert it to an integer using the int() function and print the result. If the string is not numeric, we print an error message.
Example 4: Validating PIN codes
A PIN code is typically a numeric string of a fixed length (e.g. 4 digits or 6 digits). We can use isnumeric() to check if a given string is a valid PIN code. For example:
pin = "1234"
if pin.isnumeric() and len(pin) == 4: print("The string is a valid 4-digit PIN code") else: print("The string is not a valid 4-digit PIN code")
Output:
The string is a valid 4-digit PIN code
In this example, we first use isnumeric() to check that the string consists only of numeric characters. We also check that the length of the string is 4 (which is the standard length for a 4-digit PIN code). We consider the string a valid PIN code if it passes these checks.
Example 5: Validating credit card numbers
A credit card number is typically a numeric string of a fixed length (e.g. 16 digits for a Visa or MasterCard). We can use isnumeric() to check if a given string is a valid credit card number. For example:
cc_number = "1234567890123456"
if cc_number.isnumeric() and len(cc_number) == 16: print("The string is a valid credit card number") else: print("The string is not a valid credit card number")
Output:
The string is a valid credit card number
In this example, we first use isnumeric() to check that the string consists only of numeric characters. We also check that the length of the string is 16 (which is the standard length for most credit card numbers). If the string passes these checks, we consider it a valid credit card number.
Endnotes
The isnumeric() method in Python is useful for checking if a string consists of numeric characters. It can help ensure user input is in the expected format and avoid errors when working with numerical data. While isnumeric() is the most inclusive method among isdigit() and isdecimal(), it is important to remember that it doesn’t consider formatting or other symbols like signs or currency symbols. By using the isnumeric() method effectively and understanding its limitations, Python developers can write more robust and error-free code.
Hope this article was helpful for you. You can explore related articles here if you wish to learn more about Python and practice Python programming.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio