Open In App

Python – Check if string contains any number

Last Updated : 13 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 whether a digit is present in the string or not.

Example

Input: Test_str = 'Geeks42eeks' 
Output: True 
Input: Test_str = 'GeeksForGeeks' 
Output: False
Explanation: In this, we are checking if the string contains any number or not.

Python Check if String is Integer

In Python, we can check if the string contains any number or not using different approaches. Here are different methods to use:

Check if String Contains Any Number using isdigit()

The function can be used to solve this problem. In this, we check for numbers using isdigit() and check for any occurrence using any().

Python3
# initializing string
test_str = 'geeks4geeks'

print("The original string is : " + str(test_str))

# using any() to check for any occurrence
res = any(chr.isdigit() for chr in test_str)

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)

Python Check if String is Integer using Loop

In this method, we are checking if string is an integer using loop function.

Python3
# initializing string
test_str = 'geeks4geeks'

print("The original string is : " + str(test_str))
res=False
for i in test_str:
    try:
      int(i)
      res=True
      break
    except:
      res=False

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

Python Check if String is Integer 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)

Python Check if String is Integer 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)

Python Check if String is Integer Without any Builtin Method

To check if a string represents an integer in Python without using any built-in methods, you can iterate over the characters of the string and manually check if each character is a digit.

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)

Python Check if String is Integer using operator.countOf() Method

In Python, there is no built-in method called operator.countOf() that can be directly used to check if a string represents an integer. However, we can utilize the operator.countOf() method from the operator module in a custom implementation to count the occurrences of digits in a string and use that count to determine if the string represents an integer.

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)

Python Check if String is an Integer using find() and any() Methods

In Python, you can use the find() and any() methods to check if a string represents an integer. By utilizing these methods, you can iterate over each character in the string and check if it is a digit.

Python3
#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)) != -1 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

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads