How To Check If A String Contains A Number In Python

Check if a string contains a number in Python

In Python, there are many ways to check if a string contains a number in Python. Depending on your needs and purposes, you can choose many different methods. I will guide you in this article with some basic methods to solve this problem.

Check if a string contains a number in Python?

Method 1: Use for loop

We can use for loop in Python to check if a string contains a number. Here’s how I do it:

def containsNumber(myString):
    for char in myString:
        if char.isdigit():
            return True
    return False

print(containsNumber("p3rs0n"))
print(containsNumber("pErsOn"))

Output:

True
False

In this example, I combine the for loop and isdigit() method to iterate through each input string character. If the string has a single character, the number of the loop will stop and return True. Otherwise, the line second console.log() has no numeric characters, so the code returns false.

Syntax:

string.isdigit()

We can also use it to check if a string contains a number.

Example:

print('281'.isdigit())
print('tree'.isdigit())
print(' '.isdigit())

Output:

True
False
False

If we don’t need to look at each value, we can use isdigit() to check for numeric strings faster than the for loop.

Method 2: Use re.search()

Syntax:

re.search(pat, str)

Parameters – Description

  • pat: Is a regular expression passed to search the character here is r'[0-9]’
  • str: Is the string we want to search on it

Used to find if there is a number in a string, I give the following syntax:

def containsNumber(myString):
    return bool(re.search(r'[0-9]', myString))

Here is an example:

import re

def containsNumber(myString):
    return bool(re.search(r'[0-9]', myString))
    
print(containsNumber("p3rs0n"))
print(containsNumber("pErsOn"))

Output:

True
False

The re.search() will search from the beginning. When it sees a number, it will return a match, and when converted to bool, it will be True. If not, it will return False. In the above example, the string ‘p3rs0n’ appears with two numbers, 3 and 0, after being translated by the code. The result is true, and for the second string, false is returned because ‘pErsOn’ does not have a child any number.

In addition, we also instead of checking the string with numbers from 0-9 or not, we can replace it with a string with 1-4 or 0-5 by changing the value in [] as we want.

Example:

import re

def containsNumber(myString):
    return bool(re.search(r'[1-3]', myString))
    
print(containsNumber("this is number 3"))
print(containsNumber("this is number 9"))

Output:

True
False

Summary

There are many ways to check if a string contains a number in Python. Through this article, I introduced two methods, for loop and re.search(), to solve this problem. I hope through this article, you can understand and apply it to your program. Wish you success.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *