Python - Check if String contains any Number Last Updated : 31 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article 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. Comment More infoAdvertise with us Next Article Python | Check Numeric Suffix in String M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python Check If String is Number In Python, there are different situations where we need to determine whether a given string is valid or not. Given a string, the task is to develop a Python program to check whether the string represents a valid number.Example: Using isdigit() MethodPython# Python code to check if string is numeric 6 min read Check if string contains character - Python We are given a string and our task is to check if it contains a specific character, this can happen when validating input or searching for a pattern. For example, if we check whether 'e' is in the string 'hello', the output will be True.Using in Operatorin operator is the easiest way to check if a c 2 min read Check Whether String Contains Only Numbers or Not - Python We are given a string s="12345" we need to check whether the string contains only number or not if the string contains only number we will return True or if the string does contains some other value then we will return False. This article will explore various techniques to check if a string contains 3 min read Python | Check Numeric Suffix in String Sometimes, while programming, we can have such a problem in which we need to check if any string is ending with a number i.e it has a numeric suffix. This problem can occur in Web Development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using regex This problem 6 min read Python - Test if String contains any Uppercase character The goal is to check if a given string contains at least one uppercase letter (A-Z). Using any() and isupper()any() function, combined with isdigit(), checks if any character in a string is a digit. It efficiently scans the string and returns True if at least one digit is found.Python# Define the in 3 min read Python | Ways to check if given string contains only letter Given a string, write a Python program to find whether a string contains only letters and no other keywords. Let's discuss a few methods to complete the task. Method #1: Using isalpha() method Python3 # Python code to demonstrate # to find whether string contains # only letters # Initialising string 3 min read Like