Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python – Check if string contains any number

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 : True 

Input : 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 string
test_str = 'geeks4geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using any() to check for any occurrence
res = any(chr.isdigit() for chr in test_str)
     
# printing result
print("Does string contain any digit ? : " + str(res))

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 string
test_str = 'geeks4geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# next() checking for each element, reaches end, if no element found as digit
res = True if next((chr for chr in test_str if chr.isdigit()), None) else False
 
# printing result
print("Does string contain any digit ? : " + str(res))

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)))

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 re
 
str1 = "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)))

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 False
print(digit("Geeks4Geeks"))
print(digit("GeeksForGeeks"))

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 string
test_str = 'geeks4geeks'
 
# printing original string
print("The original string is : " + str(test_str))
res=False
numbers="0123456789"
for i in test_str:
    if i in numbers:
        res=True
        break
 
# printing result
print("Does string contain any digit ? : " + str(res))

Output

The original string is : geeks4geeks
Does string contain any digit ? : True

Time Complexity: O(n)
Auxiliary Space: O(1)

Using replace() and len() methods

Python3




# Python3 code to demonstrate working of
# Check if string contains any number
 
# initializing string
test_str = 'geeks21geeks'
 
# printing original string
print("The original string is : " + str(test_str))
res = False
x = test_str
numbers = "0123456789"
for i in numbers:
    test_str = test_str.replace(i, "")
if(len(x) != len(test_str)):
    res = True
# printing result
print("Does string contain any digit ? : " + str(res))

Output

The original string is : geeks21geeks
Does string contain any digit ? : True

Using find() and any() methods

Python3




# Python3 code to demonstrate working of
# Check if string contains any number
 
#defining function to check is number present in string or not using find() and any() methods
def contains_number(string):
    return any(string.find(str(i)) for i in range(10))
 
# initializing string
test_str = 'geeks21geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
#calling contains_number function
res=contains_number(test_str)
 
# printing result
print("Does string contain any digit ? : " + str(res))

Output

The original string is : geeks21geeks
Does string contain any digit ? : True

Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Check if string contains any number
import operator as op
# initializing string
test_str = 'geeks4geeks'
 
# printing original string
print("The original string is : " + str(test_str))
res = False
digits = "0123456789"
for i in test_str:
    if op.countOf(digits, i) > 0:
        res = True
        break
 
# printing result
print("Does string contain any digit ? : " + str(res))

Output

The original string is : geeks4geeks
Does string contain any digit ? : True

Time Complexity: O(n)

Auxiliary Space: O(1)

Using loop, try and except.

Python3




# Python3 code to demonstrate working of
# Check if string contains any number
 
# initializing string
test_str = 'geeks4geeks'
 
# printing original string
print("The original string is : " + str(test_str))
res=False
for i in test_str:
    try:
      int(i)
      res=True
      break
    except:
      res=False
# printing result
print("Does string contain any digit ? : " + str(res))
#this code contributed by tvsk

Output

The original string is : geeks4geeks
Does string contain any digit ? : True

Time Complexity: O(n)

Auxiliary Space: O(1)

Another method : Using list and loop

Approach:

  1. Declare a characters list numbers consisting numbers from 0 to 9 as shown in code below.
  2. Declare ans name variable with False initially.
  3. Iterate the given string with loop.
  4. If the character is in numbers list then change ans to True and break the loop.
  5. After the loop ends, print the ans.

Python3




# Python3 code to demonstrate working of
# Check if string contains any number
 
# initializing string
test_str = 'geeks4geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
ans = False
numbers = ['0','1','2','3','4','5','6','7','8','9']
 
for i in test_str:
    if i in numbers:
        ans = True
        break
 
# printing result
print("Does string contain any digit ? : " , ans)
 
# This code is contributed by Pratik Gupta (guptapratik)

Output

The original string is : geeks4geeks
Does string contain any digit ? :  True

Time Complexity: O(n), where n is number of characters in string.

Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Last Updated : 28 Feb, 2023
Like Article
Save Article
Similar Reads
Related Tutorials