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.
How to check if a string contains a number 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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
Using a loop to check for numbers in a string
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.
Using regular expressions
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 –
- Check if a String contains a Substring in Python
- Python – Remove Non Alphanumeric Characters from String
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.