Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Check If String is Number
Next article icon

Python – Check if String contains any Number

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a string and our task is to check whether it contains any numeric digits (0-9). For example, consider the following string: s = “Hello123” since it contains digits (1, 2, 3), the output should be True while on the other hand, for s = “HelloWorld” since it has no digits the output should be False.

Using any() and isdigit():

any() function in Python returns True if any element of an iterable evaluates to True and when combined with isdigit(), it can be used to check if any character in a string is a digit.

Python
s = "abc123"  res = any(char.isdigit() for char in s)  if res:  # If 'res' is True, it means there is at least one digit in the string     print("Yes")  else:     print("No") 

Output
Yes 

Explanation:

  • any(char.isdigit() for char in s) uses a generator expression to check if any character in the string s is a digit.
  • isdigit() method returns True for digits and any() returns True if any character satisfies the condition.

Using a for loop:

A for loop can be used to iterate through each character in a string and check if any character is a digit using isdigit(). If a digit is found, a flag is set, and the result is displayed.

Python
s = "abc123"  # Flag to track if number is found flag = False  # Loop through each character and check if it's a digit for char in s:     if char.isdigit():         flag = True         break  if flag:     print("Yes") else:     print("No") 

Output
Yes 

Explanation:

  • if char.isdigit() checks if the current character char is a digit.
  • If a digit is found, flag = True sets the flag to True and break exits the loop early to stop further checks.

Using Regular Expressions:

Regular expressions (regex) in Python offer a powerful way to search for patterns within strings. The re module provides functions like re.search() to check if a string contains specific patterns such as digits. By using a regex pattern like \d, you can easily detect if any digit exists in the string.

Python
import re  s = "abc123"  # Check if the string contains any digit res = bool(re.search(r'\d', s))  if res:     print("Yes") else:     print("No") 

Output
Yes 

Explanation:

  • re.search(r’\d’, s) searches for the first digit (\d) in the string s.
  • bool(re.search(r’\d’, s)) converts the result to True if a digit is found otherwise False.


Next Article
Python Check If String is Number
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Python Check If String is Number
    In Python, there are different situations where we need to determine whether a given string is valid or not. Given a string, the task is to develop a Python program to check whether the string represents a valid number. Example: Using isdigit() Method [GFGTABS] Python # Python code to check if strin
    6 min read
  • Check if string contains character - Python
    We are given a string and our task is to check if it contains a specific character, this can happen when validating input or searching for a pattern. For example, if we check whether 'e' is in the string 'hello', the output will be True. Using in Operatorin operator is the easiest way to check if a
    2 min read
  • Check Whether String Contains Only Numbers or Not - Python
    We are given a string s="12345" we need to check whether the string contains only number or not if the string contains only number we will return True or if the string does contains some other value then we will return False. This article will explore various techniques to check if a string contains
    3 min read
  • Insert a number in string - Python
    We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num
    2 min read
  • Python | Check Numeric Suffix in String
    Sometimes, while programming, we can have such a problem in which we need to check if any string is ending with a number i.e it has a numeric suffix. This problem can occur in Web Development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using regex This problem
    6 min read
  • Python | Check if any String is empty in list
    Sometimes, while working with Python, we can have a problem in which we need to check for perfection of data in list. One of parameter can be that each element in list is non-empty. Let's discuss if a list is perfect on this factor using certain methods. Method #1 : Using any() + len() The combinati
    6 min read
  • Python - Test if String contains any Uppercase character
    The goal is to check if a given string contains at least one uppercase letter (A-Z). Using any() and isupper()any() function, combined with isdigit(), checks if any character in a string is a digit. It efficiently scans the string and returns True if at least one digit is found. [GFGTABS] Python # D
    3 min read
  • Python | Ways to check if given string contains only letter
    Given a string, write a Python program to find whether a string contains only letters and no other keywords. Let's discuss a few methods to complete the task. Method #1: Using isalpha() method C/C++ Code # Python code to demonstrate # to find whether string contains # only letters # Initialising str
    3 min read
  • Check if String is Empty or Not - Python
    We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example: Using Comparison Operator(==)The s
    2 min read
  • Check for ASCII String - Python
    To check if a string contains only ASCII characters, we ensure all characters fall within the ASCII range (0 to 127). This involves comparing each character's value to ensure it meets the criteria. Using str.isascii()The simplest way to do this in Python is by using the built-in str.isascii() method
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences