In this article, I present 7 Python scripts that can be used to determine if a string contains nothing but digits 0 – 9.
All 7 scripts work for uncomplicated strings – those that don’t have unusual or non-English characters, and do not contain spaces. To understand the idiosyncrasies of each script, see the caution section in the script’s explanation. Scripts 5, 6 and 7 are most likely the least problematic for this purpose.
Script 1: text.isdecimal()
Script: This script returns True if the string is not empty and if the character(s) of the string are one or more of these digits: 1234567890
.
Caution: This script returns True for digits in other languages such as: Osmanya digit 7: ?, Thai digit 4: ๔, Tibetan digit 5: ༥, Lao digit 8: ໘, or Arabic digit 3: ٣.
############################# ## Script 1 ## text.isdecimal() ############################# def tests_presence_of_only_digits_in_text(text): return text.isdecimal(): '1234567890' # True: Only digits '123abc456d' # False: Digits and letters '123",.([)#' # False: Digits and punctuation marks 'afghQrxbWz' # False: Capital and lower-case letters ' ' # False: Only spaces '123456789 0'# False: Digits and space # Special cases '?๔༥໘٣෮೮౪௭୩' # True: 0 - 9 characters in other languages '⁰³' # False: Superscript characters '?' # False: Kharosthi character '1/2' # False: Vulgar Fraction '' # False: Empty text
Script 2: text.isdigit()
Script: This script returns True if the string is not empty and if the character(s) of the string are one or more of these digits: 1234567890.
Caution: This script returns True for all the characters identified by isdecimal()
as well as superscript characters such as ³, and Kharosthi characters such as: ? ? ?.
############################# ## Script 2 ## text.isdigit() ############################# def tests_presence_of_only_digits_in_text(text): return text.isdigit() '1234567890' # True: Only digits '123abc456d' # False: Digits and letters '123",.([)#' # False: Digits and punctuation marks 'afghQrxbWz' # False: Capital and lower-case letters ' ' # False: Only spaces '123456789 0'# False: Digits and space # Special cases '?๔༥໘٣෮೮౪௭୩' # True: 0 - 9 characters in other languages '⁰³' # True: Superscript characters '?' # True: Kharosthi character '1/2' # False: Vulgar Fraction '' # False: Empty text
Script 3: text.isnumeric()
Script: This script returns True if the string is not empty and if the character(s) of the string are one or more of these digits: 1234567890.
Caution: This script returns True for all characters identified by isdecimal()
and isdigit()
as well as special numerical characters such as ½.
############################# ## Script 3 ## text.isnumeric() ############################# def tests_presence_of_only_digits_in_text(text): if text.isnumeric(): return True else: return False '1234567890' # True: Only digits '123abc456d' # False: Digits and letters '123",.([)#' # False: Digits and punctuation marks 'afghQrxbWz' # False: Capital and lower-case letters ' ' # False: Only spaces '123456789 0'# False: Digits and space # Special cases '?๔༥໘٣෮೮౪௭୩' # True: 0 - 9 characters in other languages '⁰³' # True: Superscript characters '?' # True: Kharosthi character '1/2' # True: Vulgar Fraction '' # False: Empty text
Script 4: ValueError
Script: This script uses error handling and returns True when no character other than 1234567890 is present in the text. The script uses the int()
method to turn string to integer. It throws a ValueError
and returns False if any character other than digits 0 – 9 are present.
Caution: This script returns True for digits in other languages such as: Osmanya digit 7: ?, Thai digit 4: ๔, Tibetan digit 5: ༥, Lao digit 8: ໘, or Arabic digit 3: ٣
############################ # Script 4 # ValueError ############################ def tests_presence_of_only_digits_in_text(text): try: num = int(text) return True except ValueError: return False '1234567890' # True: Only digits '123abc456d' # False: Digits and letters '123",.([)#' # False: Digits and punctuation marks 'afghQrxbWz' # False: Capital and lower-case letters ' ' # False: Only spaces '123456789 0'# False: Digits and space # Special cases '?๔༥໘٣෮೮౪௭୩' # True: 0 - 9 characters in other languages '⁰³' # False: Superscript characters '?' # False: Kharosthi character '1/2' # False: Vulgar Fraction '' # False: Empty text
Script 5: re.match(‘pattern’, ‘text’)
Script: This script uses the regular expression re
module and returns the match object if zero or more characters at the beginning of string match the regular expression pattern. The asterisk * causes the regular expression code to match 0 or more repetitions of the preceding expression.
Caution: You must compensate for an empty string. If the string is empty, re.match()
will return True.
############################ # Script 5 # re.match() ############################ import re def tests_presence_of_only_digits_in_text(text): if len(text) == 0: return False else: if re.match('[0-9]*$', text): return True else: return False '1234567890' # True: Only digits '123abc456d' # False: Digits and letters '123",.([)#' # False: Digits and punctuation marks 'afghQrxbWz' # False: Capital and lower-case letters ' ' # False: Only spaces '123456789 0'# False: Digits and space # Special cases '?๔༥໘٣෮೮౪௭୩' # False: 0 - 9 characters in other languages '⁰³' # False: Superscript characters '?' # False: Kharosthi character '1/2' # False: Vulgar Fraction '' # False: Empty text
Script 6: re.search(‘pattern’, ‘text’)
Script: This script uses the re
module and returns the match object if zero or more characters at the beginning of string match the regular expression pattern. Again, the asterisk operator * causes the regular expression code to match 0 or more repetitions of the preceding expression. Notice the small difference between the code for re.match()
in Script 5 and re.search()
in this script. re.search()
finds a match anywhere in the text, but the addition of ^
restricts the match to the beginning of the text.
Caution: You must compensate for an empty string. If the string is empty, re.search()
will return True.
############################ # Script 6 # re.search() ############################ import re def tests_presence_of_only_digits_in_text(text): if len(text) == 0: return False else: # ^ restricts the match to the beginning of the string. if re.search('^[0-9]*$', text): return True else: return False '1234567890' # True: Only digits '123abc456d' # False: Digits and letters '123",.([)#' # False: Digits and punctuation marks 'afghQrxbWz' # False: Capital and lower-case letters ' ' # False: Only spaces '123456789 0'# False: Digits and space # Special cases '?๔༥໘٣෮೮౪௭୩' # False: 0 - 9 characters in other languages '⁰³' # False: Superscript characters '?' # False: Kharosthi character '1/2' # False: Vulgar Fraction '' # False: Empty text
Script 7: string.digits
Script: This script uses the string module and the string.digits constant – 0123456789. The script returns True if the characters in the string match the characters in string.digits. If an unmatched character is encountered, the script returns False.
Caution: You must compensate for an empty string. If the string is empty, this script will return True. You can use the break command to leave the for loop after the first unmatched character is found.
############################ # Script 7 # string.digits ############################ import string def tests_presence_of_only_digits_in_string(text): if len(text) == 0: return False else: count = 0 for ch in text: if ch not in string.digits: count += 1 if count > 0: return False else: return True '1234567890' # True: Only digits '123abc456d' # False: Digits and letters '123",.([)#' # False: Digits and punctuation marks 'afghQrxbWz' # False: Capital and lower-case letters ' ' # False: Only spaces '123456789 0'# False: Digits and space # Special cases '?๔༥໘٣෮೮౪௭୩' # False: 0 - 9 characters in other languages '⁰³' # False: Superscript characters '?' # False: Kharosthi character '1/2' # False: Vulgar Fraction '' # False: Empty string
Conclusion
In writing software, there are usually more than one approach to solve a problem. The 7 scripts above present some of the ways to ascertain if a text contains digits and nothing else.
If you find an error in this article, please communicate with me. I will correct it and will mention your name in the revised article.
Please write to: knourian@outlook.com.