Python program to check if a string has at least one letter and one number
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.
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
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.
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.
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.
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.