Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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 program to check if a string has at least one letter and one number
Next article icon

Python program to check if a string has at least one letter and one number

Last Updated : 01 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task is to verify whether a given string contains both at least one letter (either uppercase or lowercase) and at least one number. For example, if the input string is "Hello123", the program should return True since it contains both letters and numbers. On the other hand, a string like "Hello" or "12345" would return False because it lacks either letters or numbers.

Using any()

any() returns True if at least one element in an iterable is True. We can use this to check if a string contains at least one letter and one number. The isalpha() method helps check for letters and the isdigit() method checks for digits.

Python
s = "geeksforgeeks"  # Check if the `s` contains at least one letter l= any(c.isalpha() for c in s)  # Check if the `s` contains at least one number n= any(c.isdigit() for c in s)  # If both conditions are met if l and n:     print(True)  else:     print(False) 

Output
False 

Explanation:

  • any(c.isalpha() for c in s) checks if the string contains at least one letter.
  • any(c.isdigit() for c in s) checks if the string contains at least one digit.
  • If both l (letter found) and n (digit found) are True print True, otherwise False.

Let's understand different method to check if a string has at least one letter and one number.

Table of Content

  • Using Regular Expression
  • Using Built-in String Methods Like set.isdisjoint()
  • Using for loop

Using Regular Expression

Regular expressions (regex) allow for pattern matching in strings. We can use re.search() to check for the presence of both letters and digits. The pattern [a-zA-Z] checks for any letter, while \d checks for any digit.

Python
import re s = "Python123"  # checks for lettors l = bool(re.search(r'[a-zA-Z]', s))   # checks for digits  n = bool(re.search(r'\d', s))    if l and n:     print(True) else:     print(False) 

Output
True 

Explanation:

  • regex pattern [a-zA-Z] matches any letter and \d matches any digit.
  • re.search() finds the first match of these patterns in the string and If both a letter and a digit are found print True, otherwise False.

Using Built-in String Methods Like set.isdisjoint()

isdisjoint() method checks if two sets have no elements in common, so we check whether the set of characters in the string intersects with the set of letters and digits.

Python
s = "Python123" letters = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") digits  = set("0123456789")  char = set(s) print(not char.isdisjoint(letters) and not char.isdisjoint(digits)) 

Output
True 

Explanation:

  • Create two sets, one for letters (letters) and one for digits (digits) and convert the input string s into a set of characters (char).
  • isdisjoint() check if the string has common elements with both the letters and digits sets. If both checks return True, the string contains both letters and digits.

Using for loop

A simple approach is to iterate through the string and check each character to see if it’s a letter or a number. We can keep track of whether we’ve found both a letter and a number and return True as soon as both are found.

Python
s = "gfg123" # Initialize flags for letter and digit presence l = d= False  # Iterate through the string for char in s:     if char.isalpha():         l = True     if char.isdigit():         d= True     # If both letter and digit are found, print True and exit     if l and d:         print(True)         break else:     print(False) 

Output
True 

Explanation:

  • Loop through the string and check if each character is a letter (char.isalpha()) or a digit (char.isdigit()).
  • If both conditions are met, print True and exit otherwise print False after the loop ends.

Next Article
Python program to check if a string has at least one letter and one number

S

simran_bhandari
Improve
Article Tags :
  • Python
  • Python Programs
  • python-string
  • Python-string-functions
  • Python string-programs
Practice Tags :
  • python

Similar Reads

    Python program to calculate the number of digits and letters in a string
    In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
    3 min read
    Python program to check if lowercase letters exist in a string
    Checking for the presence of lowercase letters involves scanning the string and verifying if at least one character is a lowercase letter (from 'a' to 'z').Python provides simple and efficient ways to solve this problem using built-in string methods and constructs like loops and comprehensions.Using
    2 min read
    Python Program to check whether all elements in a string list are numeric
    Given a list that contains only string elements the task here is to write a Python program to check if all of them are numeric or not. If all are numeric return True otherwise, return False. Input : test_list = ["434", "823", "98", "74"] Output : True Explanation : All Strings are digits.Input : tes
    5 min read
    Python program to calculate the number of words and characters in the string
    We are given a string we need to find the total number of words and total number of character in the given string.For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string. I
    3 min read
    Python Program to test if the String only Numbers and Alphabets
    Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations. Examples: Input : test_str = 'Geeks4Geeks' Output : True Explanation : Contains both number and alphabets. Input : test_str = 'GeeksforGeeks' Output : False Expl
    4 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