Check if a String Contains Only Digits

problem-solving
Published

September 27, 2023

Python offers several elegant ways to determine if a string comprises solely digits. This capability is crucial in various applications, from data validation and input sanitization to parsing numerical data from text. This post explores different methods, comparing their efficiency and readability.

Method 1: Using isdigit()

The most straightforward approach leverages the built-in string method isdigit(). This method returns True if all characters in the string are digits, and False otherwise.

def is_digit_string_isdigit(input_string):
  """Checks if a string contains only digits using isdigit().

  Args:
    input_string: The string to check.

  Returns:
    True if the string contains only digits, False otherwise.
  """
  return input_string.isdigit()

print(is_digit_string_isdigit("12345"))  # Output: True
print(is_digit_string_isdigit("123a45")) # Output: False
print(is_digit_string_isdigit("12 345")) # Output: False
print(is_digit_string_isdigit(""))       # Output: False

This method is concise and highly readable, making it ideal for most scenarios.

Method 2: Using Regular Expressions

The re module provides powerful pattern-matching capabilities. We can use a regular expression to check if the entire string matches a pattern consisting only of digits.

import re

def is_digit_string_regex(input_string):
  """Checks if a string contains only digits using regular expressions.

  Args:
    input_string: The string to check.

  Returns:
    True if the string contains only digits, False otherwise.
  """
  return bool(re.fullmatch(r"\d+", input_string))

print(is_digit_string_regex("12345"))  # Output: True
print(is_digit_string_regex("123a45")) # Output: False
print(is_digit_string_regex("12 345")) # Output: False
print(is_digit_string_regex(""))       # Output: False

While more verbose, regular expressions offer flexibility for more complex pattern matching needs beyond just digits. For simple digit checks, isdigit() is generally preferred for its simplicity.

Method 3: Looping and isdigit()

For a more manual approach (though less efficient than the previous methods), you can iterate through the string and check each character individually using isdigit().

def is_digit_string_loop(input_string):
  """Checks if a string contains only digits using a loop and isdigit().

  Args:
    input_string: The string to check.

  Returns:
    True if the string contains only digits, False otherwise.
  """
  for char in input_string:
    if not char.isdigit():
      return False
  return True

print(is_digit_string_loop("12345"))  # Output: True
print(is_digit_string_loop("123a45")) # Output: False
print(is_digit_string_loop("12 345")) # Output: False
print(is_digit_string_loop(""))       # Output: True

This method is less efficient than isdigit() but demonstrates the underlying logic. Avoid this method unless you have a specific reason for character-by-character processing.

Choosing the Right Method

For most use cases, the isdigit() method provides the optimal balance of readability and efficiency. Regular expressions are valuable when dealing with more intricate patterns, while the looping approach is generally less efficient and should be avoided unless absolutely necessary. Remember to handle edge cases like empty strings appropriately in your chosen method.