• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if String Contains Numbers in Python

Check if String Contains Numbers in Python

July 25, 2022 Leave a Comment

To check if a string contains numbers in Python, you can create a function, loop over the string and check if any of the characters are numeric with isnumeric().

a = "hello1"
b = "bye"
c = "123"

def containsNumbers(s):
    contains = False
    for char in s:
        if isnumeric(char):
            contains = True
    return contains

print(containsNumbers(a))
print(containsNumbers(b))
print(containsNumbers(c))

#Output:
True
False
True

You can also use the isdigit() function.

a = "hello1"
b = "bye"
c = "123"

def containsNumbers(s):
    contains = False
    for char in s:
        if char.isdigit():
            contains = True
    return contains

print(containsNumbers(a))
print(containsNumbers(b))
print(containsNumbers(c))

#Output:
True
False
True

When working with strings in Python, the ability to check these strings for certain conditions is very valuable.

One such case is if you want to check if a string variable contains numbers or not.

To check if a string contains numbers in Python, you can create a function, loop over the string and check if any of the characters are numeric with isnumeric().

isnumeric() allows us to check if a string is numeric or not, i.e. a number between 0 to 9.

If one of the characters in a given string is numeric, then we can conclude that our string contains numbers.

Below is a function which will check if a string contains numbers in Python.

a = "hello1"
b = "bye"
c = "123"

def containsNumbers(s):
    contains = False
    for char in s:
        if isnumeric(char):
            contains = True
    return contains

print(containsNumbers(a))
print(containsNumbers(b))
print(containsNumbers(c))

#Output:
True
False
True

Checking if String Contains Number with isdigit() in Python

Another method you can use to check if a string contains any number is to use the isdigit() function in a loop.

We can take the function from above and just adjust the line where we check if the character is numeric.

isdigit() returns True if all characters in a string are numbers.

Below is a function which will check if a string contains numbers in Python with isdigit().

a = "hello1"
b = "bye"
c = "123"

def containsNumbers(s):
    contains = False
    for char in s:
        if char.isdigit():
            contains = True
    return contains

print(containsNumbers(a))
print(containsNumbers(b))
print(containsNumbers(c))

#Output:
True
False
True

Checking If String Does Not Contain Numbers in Python

If you want to check if a string does not contain numbers in Python, then you can modify the function from above slightly.

To check if a string doesn’t contain numbers, we want to negate the ‘contains’ variable.

Below is a different function which will check if a string does not contain numbers in Python.

a = "hello1"
b = "bye"
c = "123"

def doesNotContainNumbers(s):
    doesNotContain = True
    for char in s:
        if isnumeric(char):
            doesNotContain = False
    return doesNotContain

print(containsNumbers(a))
print(containsNumbers(b))
print(containsNumbers(c))

#Output:
False
True
False

Hopefully this article has been useful for you to learn how to check if a string contains numbers or not.

Other Articles You'll Also Like:

  • 1.  Using Python to Count Items in List Matching Criteria
  • 2.  Using Python to Check if Number is Divisible by Another Number
  • 3.  Using Python to Check if String Contains Only Letters
  • 4.  How to Check if List is Empty in Python
  • 5.  Count Letters in Word in Python
  • 6.  Python turtle dot() – Draw Dot on Turtle Screen
  • 7.  Replace Values in Dictionary in Python
  • 8.  How to Remove Vowels from a String in Python
  • 9.  Using Python to Add Items to Set
  • 10.  Using Python to Read Random Line from File

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy