
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if a unicode string contains only numeric characters in Python?
In Python, Unicode strings can contain numeric characters from various languages and scripts. To check if a Unicode string contains only numeric characters, we can use built-in string methods, regular expressions, or character iteration.
These methods ensure that characters like Arabic numerals, Chinese digits, or superscripts are also recognized as valid numeric characters.
Using isnumeric() Method
The isnumeric() method returns True if all characters in the string are numeric, including Unicode numeric characters like ², ?, ?, etc. If any character is non-numeric or the string is empty, it returns False.
Example: Unicode Numeric Characters
In the following example, the string contains Devanagari digits which are valid Unicode numeric characters, so the isnumeric() function returns True -
str1 = "?????" # Hindi numerals (Devanagari) print("The given string is") print(str1) print("Checking if the string contains only numeric characters") print(str1.isnumeric())
The output is shown below -
The given string is ????? Checking if the string contains only numeric characters True
Example: Mixed Characters
In the following example, the string includes a Latin digit and a non-numeric letter. So, the isnumeric() function returns False -
str2 = "123a" print("The given string is") print(str2) print("Checking if the string contains only numeric characters") print(str2.isnumeric())
The output is -
The given string is 123a Checking if the string contains only numeric characters False
Using Regular Expressions
You can use the re.fullmatch() function to match the string against a Unicode digit pattern. The pattern \d+ matches only ASCII digits, but by using the Unicode flag, it can match other digit characters too.
Example: Using Unicode-aware Regex
In the following example, the string contains Tamil digits. Since we are using the re.fullmatch() function with the Unicode flag, it returns True -
import re str3 = "???" # Tamil numerals print("The given string is") print(str3) print("Checking if the string contains only numeric characters") print(bool(re.fullmatch(r"\d+", str3)))
The result is -
The given string is ??? Checking if the string contains only numeric characters True
Using Character-wise Check with unicodedata
The unicodedata module helps to identify if each character in a string is a numeric character or not by checking its Unicode category.
Example: Checking Character by Character
In the following example, we check each character using the unicodedata.numeric() function. If all characters can be converted to a number, the result is True -
import unicodedata def is_all_numeric(text): try: for char in text: unicodedata.numeric(char) return True except (TypeError, ValueError): return False str4 = "???" # Arabic numerals print("The given string is") print(str4) print("Checking if the string contains only numeric characters") print(is_all_numeric(str4))
Output is as follows -
The given string is ??? Checking if the string contains only numeric characters True