Python - Check if String contains any Number Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 5 Likes Like Report We are given a string and our task is to check whether it contains any numeric digits (0-9). For example, consider the following string: s = "Hello123" since it contains digits (1, 2, 3), the output should be True while on the other hand, for s = "HelloWorld" since it has no digits the output should be False.Using any() and isdigit():any() function in Python returns True if any element of an iterable evaluates to True and when combined with isdigit(), it can be used to check if any character in a string is a digit. Python s = "abc123" res = any(char.isdigit() for char in s) if res: # If 'res' is True, it means there is at least one digit in the string print("Yes") else: print("No") OutputYes Explanation:any(char.isdigit() for char in s) uses a generator expression to check if any character in the string s is a digit. isdigit() method returns True for digits and any() returns True if any character satisfies the condition.Using a for loop:A for loop can be used to iterate through each character in a string and check if any character is a digit using isdigit(). If a digit is found, a flag is set, and the result is displayed. Python s = "abc123" # Flag to track if number is found flag = False # Loop through each character and check if it's a digit for char in s: if char.isdigit(): flag = True break if flag: print("Yes") else: print("No") OutputYes Explanation:if char.isdigit() checks if the current character char is a digit.If a digit is found, flag = True sets the flag to True and break exits the loop early to stop further checks.Using Regular Expressions:Regular expressions (regex) in Python offer a powerful way to search for patterns within strings. The re module provides functions like re.search() to check if a string contains specific patterns such as digits. By using a regex pattern like \d, you can easily detect if any digit exists in the string. Python import re s = "abc123" # Check if the string contains any digit res = bool(re.search(r'\d', s)) if res: print("Yes") else: print("No") OutputYes Explanation:re.search(r'\d', s) searches for the first digit (\d) in the string s. bool(re.search(r'\d', s)) converts the result to True if a digit is found otherwise False. Create Quiz Comment M manjeet_04 Follow 5 Improve M manjeet_04 Follow 5 Improve Article Tags : Python Python Programs Python string-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like