Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Check if a Python String Contains Only Digits?

#1
How to Check if a Python String Contains Only Digits?

In this article, I will 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 texts – those that don’t have unusual characters, spaces, non-English characters, etc. 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 ones 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 returns True if the characters in the text match the characters produced by string.digits, which are 0123456789. If even one character does not match, the script returns False

Caution: You can use break to leave the loop after the first unmatched character was found.

############################
# Script 7
# string.digits
############################ import string def tests_presence_of_only_digits_in_text(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 text

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.

The post How to Check if a Python String Contains Only Digits? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/04/...ly-digits/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Check if the Current Time is in a Range in Python? xSicKxBot 0 12 05-10-2021, 02:53 PM
Last Post: xSicKxBot
  [Tut] Python Regex to Return String Between Parentheses xSicKxBot 0 15 05-04-2021, 05:21 PM
Last Post: xSicKxBot
  [Tut] How To Check if One String is a Subsequence of Another? xSicKxBot 0 31 04-22-2021, 05:00 PM
Last Post: xSicKxBot
  [Tut] Converting Integer to String in Python xSicKxBot 0 35 04-18-2021, 11:56 AM
Last Post: xSicKxBot
  [Tut] How To Remove All Non-Alphabet Characters From A String? xSicKxBot 0 31 04-17-2021, 09:56 PM
Last Post: xSicKxBot
  [Tut] How to Convert a String List to an Integer List in Python xSicKxBot 0 41 04-09-2021, 01:50 PM
Last Post: xSicKxBot
  [Tut] How to Convert a Float List to a String List in Python xSicKxBot 0 39 04-07-2021, 02:53 PM
Last Post: xSicKxBot
  [Tut] Python – How to Check if a Dict Key Exists? xSicKxBot 0 34 04-03-2021, 11:11 AM
Last Post: xSicKxBot
  [Tut] Python String to Float Conversion xSicKxBot 0 40 04-01-2021, 11:07 PM
Last Post: xSicKxBot
  [Tut] How to Convert Hex String to Integer in Python xSicKxBot 0 44 03-30-2021, 01:04 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
News - F4F Reveals Banjo-Kazooie Mumbo J...
Last Post: xSicKxBot
Yesterday 11:16 PM
» Replies: 0
» Views: 3
News - Review Roundup For Spiral: From T...
Last Post: xSicKxBot
Yesterday 11:16 PM
» Replies: 0
» Views: 15
Black Ops 2 Box and Name ESP | Fully Ext...
Last Post: Jaesanta
Yesterday 10:34 PM
» Replies: 55
» Views: 20949
[Tut] [FANG KILLER ICP] Will the Interne...
Last Post: xSicKxBot
Yesterday 05:04 PM
» Replies: 0
» Views: 9
(Indie Deal) Star Wars, No Man's Sky, Co...
Last Post: xSicKxBot
Yesterday 05:04 PM
» Replies: 0
» Views: 10
Unity Mega Bundle 2021 On Now
Last Post: xSicKxBot
Yesterday 05:04 PM
» Replies: 0
» Views: 11
Mobile - Noob Army Tycoon codes – free m...
Last Post: xSicKxBot
Yesterday 05:04 PM
» Replies: 0
» Views: 10
AppleInsider - UK repair firm fined $147...
Last Post: xSicKxBot
Yesterday 05:04 PM
» Replies: 0
» Views: 10
Microsoft - Securing a new world of hybr...
Last Post: xSicKxBot
Yesterday 05:04 PM
» Replies: 0
» Views: 9
Fedora - Using Ansible to configure Podm...
Last Post: xSicKxBot
Yesterday 05:04 PM
» Replies: 0
» Views: 10

[-]
Twitter

Copyright © SickGaming.net 2012-2020