Check if a string contains a number in Python

avatar

Borislav Hadzhiev

Last updated: Jul 13, 2022

banner

Photo from Unsplash

Check if a string contains a number in Python #

To check if a string contains a number in Python:

  1. Use a generator expression to iterate over the string.
  2. Use the str.isdigit() method to check if each char is a digit.
  3. Pass the result to the any() function.
  4. The any function will return True if the string contains a number.
main.py
def contains_number(string): return any(char.isdigit() for char in string) print(contains_number('abc123')) # 👉️ True print(contains_number('abc')) # 👉️ False print(contains_number('-1abc')) # 👉️ True # ----------------------------- # ✅ check if string contains specific number print('123' in 'abc123') # 👉️ true print('567' in 'abc123') # 👉️ False
If you'd rather use a regular expression, scroll down to the next subheading.

We used a generator expression to iterate over hte string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.isdigit() method to check if the character is a digit.

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

The last step is to pass the generator object to the any() function.

main.py
def contains_number(string): return any(char.isdigit() for char in string) print(contains_number('abc123')) # 👉️ True print(contains_number('abc')) # 👉️ False print(contains_number('-1abc')) # 👉️ True

The any function takes an iterable as an argument and returns True if any element of the iterable is truthy.

The any() function will return True if at least 1 character in the string is a number.

If you need to check if a string contains a specific number, use the in operator.

main.py
print('123' in 'abc123') # 👉️ True print('567' in 'abc123') # 👉️ False

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

Check if a string contains a number using regex #

To check if a string contains a number in Python:

  1. Use the re.search() method to check if the string contains any digits.
  2. Use the bool() class to convert the output from re.search to a boolean.
main.py
import re def contains_number(string): return bool(re.search(r'\d', string)) print(contains_number('abc123')) # 👉️ True print(contains_number('abc')) # 👉️ False print(contains_number('-1abc')) # 👉️ True

The re.search method looks for the first location in the string where the provided regular expression produces a match.

If the re.search() method finds any digits, it will return amatch object, otherwise None is returned.

The first argument we passed to the method is a regular expression.

The \d character matches the digits [0-9] (and many other digit characters).

If the string contains any digits, the re.search method will return a match object, which when converted to boolean returns True.

main.py
# 👇️ <re.Match object; span=(3, 4), match='1'> print(re.search(r'\d', 'abc123')) # 👇️ None print(re.search(r'\d', 'abc'))

If the pattern is not matched in the string, the method returns None, which is a falsy value.

An alternative to using the \d character is to specify a set of digits [0-9].

main.py
import re def contains_number(string): return bool(re.search(r'[0-9]', string)) print(contains_number('abc123')) # 👉️ True print(contains_number('abc')) # 👉️ False print(contains_number('-1abc')) # 👉️ True

The square [] brackets are used to indicate a set of characters.

In the case of [0-9], the pattern matches any digit.

If you ever need help reading or writing a regular expression, consult the regular expression syntax subheading in the official docs.

The page contains a list of all of the special characters with many useful examples.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.