Check if string contains numbers in python

Python – Check if a String Contains Numbers

In this tutorial, we will look at how to check whether a string contains numbers or not in Python with the help of some examples.

Check if string contains numbers in python

You can use a combination of the built-in any() function and the string isdigit() function to check if a string contains any numbers in Python. The following is the syntax –

# check for presence of numerical characters in string s
any(ch.isdigit() for ch in s)

It returns True if any of the characters in the string is a numerical character.

Let’s look at an example –

# create a string
s = "Jarvis 123"
# check if any numbers in s
print(any(ch.isdigit() for ch in s))

Output:

True

We get True as the output, as the string does, in fact, contain numbers. Note that the any() function in Python returns True if any of the values inside the passed iterable is True.

Let’s see an example where a string doesn’t contain any numerical characters.

# create a string
s = "Jarvis abc"
# check if any numbers in s
print(any(ch.isdigit() for ch in s))

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

False

We get False as the output.

There are other methods as well that you can use to check whether a string contains numbers or not. Let’s look at some of them.

In this method, we iterate over each character in the string and check whether it is a numerical character or not.

If we find a numerical character in the string, we return True, else we return False. Let’s look at an example –

# check for presence of character in string
def has_digits(s):
    for ch in s:
        if ch.isdigit():
            return True
    return False

# create a string
s = "Jarvis 123"
# use the has_digits() function
print(has_digits(s))

Output:

True

We get True as the output as the string in the above example did contain a numerical character.

You can also use regular expressions to match for numerical characters in the string. If there are no matches then the string does not contain any numerical characters.

Let’s look at an example.

import re

def has_digits(s):
    # match for numerical characters using regex
    matches = re.findall(r'[0-9]', s)
    # does the string contain any numbers?
    if len(matches) > 0:
        return True
    else:
        return False
    
# create a string
s = "Jarvis 123"
# use the has_digits() function
print(has_digits(s))

Output:

True

We get True as the output because the regular expression matched with numerical characters in the string.

Here’s the code to see the matches if you’re interested in knowing what’s happening under the hood.

import re

# create string
s = "Jarvis 123"
# match for numerical characters using regex
matches = re.findall(r'[0-9]', s)
# display the matches
print(matches)

Output:

['1', '2', '3']

You can see that the regular expression matched with all the numerical characters in the string.

For more on regular expressions in Python, refer to this guide.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top