Python – Check if string contains any number
In this article, we will check How to check if a string contains a number in Python, we are given a string and we have to return a boolean result i.e. True or False stating that whether a digit is present in the string or not.
Input : test_str = ‘geeks4g2eeks’
Output : TrueInput : test_str = ‘geeksforgeeks’
Output : False
Check if string contains any number using any() + isdigit()
The combination of the above functions can be used to solve this problem. In this, we check for numbers using isdigit() and check for any occurrence using any().
Python3
# Python3 code to demonstrate working of# Check if string contains any number# Using isdigit() + any()# initializing stringtest_str = 'geeks4geeks'# printing original stringprint("The original string is : " + str(test_str))# using any() to check for any occurrenceres = any(chr.isdigit() for chr in test_str) # printing resultprint("Does string contain any digit ? : " + str(res)) |
The original string is : geeks4geeks Does string contain any digit ? : True
Output:
The original string is : geeks4geeks Does string contain any digit ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Check if string contains any number using next() + generator expression + isdigit()
This is yet another way in which this task can be performed. This is recommended in cases of larger strings, the iteration in the generator is cheap, but construction is usually inefficient.
Python3
# Python3 code to demonstrate working of# Check if string contains any number# Using isdigit() + next() + generator expression# initializing stringtest_str = 'geeks4geeks'# printing original stringprint("The original string is : " + str(test_str))# next() checking for each element, reaches end, if no element found as digitres = True if next((chr for chr in test_str if chr.isdigit()), None) else False# printing resultprint("Does string contain any digit ? : " + str(res)) |
The original string is : geeks4geeks Does string contain any digit ? : True
Output:
The original string is : geeks4geeks Does string contain any digit ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Check if string contains any number using the map() function
In this example, we will see if the string contains any number or not using the map function in Python. The map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable.
Python3
str1 = "Geeks"str2 = "for345"str3 = "Geeks"print(any(map(str.isdigit, str1)))print(any(map(str.isdigit, str2)))print(any(map(str.isdigit, str3))) |
False True False
Output:
False True False
Time Complexity: O(n)
Auxiliary Space: O(1)
Check if string contains any number using re.search(r’\d’)
In this example, we will use the regular expression to check if the string contains the digit or not. A regular expression is a powerful tool for matching text, based on a pre-defined pattern. To check the digit, regular expression use \d which matches the digit character.
Python3
import restr1 = "Geek3s"str2 = "for345"str3 = "Geeks"print(bool(re.search(r'\d', str1)))print(bool(re.search(r'\d', str2)))print(bool(re.search(r'\d', str3))) |
True True False
Output:
True True False
Time Complexity: O(n)
Auxiliary Space: O(1)
Check if string contains any number using the ASCII code
Here we are iterating over the entire string and comparing each character with the given range of ASCII values and finding if the character is a digit or not.
Python3
def digit(s): for i in s: if ord(i)>=48 and ord(i)<=57: return True return Falseprint(digit("Geeks4Geeks"))print(digit("GeeksForGeeks")) |
True False
Output:
True False
Time Complexity: O(n)
Auxiliary Space: O(1)
Without any builtin methods
Python3
# Python3 code to demonstrate working of# Check if string contains any number# initializing stringtest_str = 'geeks4geeks'# printing original stringprint("The original string is : " + str(test_str))res=Falsenumbers="0123456789"for i in test_str: if i in numbers: res=True break# printing resultprint("Does string contain any digit ? : " + str(res)) |
The original string is : geeks4geeks Does string contain any digit ? : True
Using replace() and len() methods
Python3
# Python3 code to demonstrate working of# Check if string contains any number# initializing stringtest_str = 'geeks21geeks'# printing original stringprint("The original string is : " + str(test_str))res = Falsex = test_strnumbers = "0123456789"for i in numbers: test_str = test_str.replace(i, "")if(len(x) != len(test_str)): res = True# printing resultprint("Does string contain any digit ? : " + str(res)) |
The original string is : geeks21geeks Does string contain any digit ? : True






Please Login to comment...