Check if a string contains a number in Python

In this article, we will discuss different ways to check if a string contains a number or not in Python.

Table of Contents

Suppose we have two strings,

"The hidden number is 22 today."
"The is sample text."

The first string contains a number, whereas the second string does not contain any number. We want to detect this using code. There are different ways to check if a string contains a number or digit. Let’s discuss them one by one

Check if a string contains a number using any() and List Comprehension

We can iterate over all characters of a string one by one using the list comprehension and a build a boolean list. During iteration, for each character we can check if it is a digit or not. If yes, then add True in the list, else add False. Then using the any() function, we can check if the list contains any True value. If yes, then it means the string contains a number. For example,

Advertisements
sampleStr = "The hidden number is 22 today."

# Check if string contains any number
result = any([ch.isdigit() for ch in sampleStr])

if result:
    print('Yes, string contains a number')
else:
    print('No, string does not contain any number')

Output:

Yes, string contains a number

It confirmed that the string contains a number.

Check if a string contains a number using Regex

The search() function of the regex module accepts a pattern and a string as arguments. Then it looks into the given string and tries to find a match to the given pattern. If match is found, then it returns a Match object, otherwise returns None. We can use the regex pattern “[0-9]” with the search() function to look for any digit in the string. For example,

import re

sampleStr = "The hidden number is 22 today."

# Check if string contains any number
result = re.search("[0-9]", sampleStr)

if result:
    print('Yes, string contains a number')
else:
    print('No, string does not contain any number')

Output:

Yes, string contains a number

It confirmed that the string contains a number.

Check if a string contains a number using any() & map()

We can pass the str.isdigit and a given string as arguments in the map() function. It will apply the isdigit() function to each character of the string and returns an iterator pointing to the returned boolean values. Pass that to the any() function to check if it contains any True value. If yes, then it means the string contains a number. For example,

sampleStr = "The hidden number is 22 today."

# Check if string contains any number
result = any(map(str.isdigit, sampleStr))

if result:
    print('Yes, string contains a number')
else:
    print('No, string does not contain any number')

Output:

Yes, string contains a number

It confirmed that the string contains a number.

Summary

We learned different ways to check if a string contains a number or not in Python. Happy Learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top