![]() |
VOOZH | about |
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.
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.
False
Explanation:
Let's understand different method to check if a string has at least one letter and one number.
Table of Content
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.
True
Explanation:
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.
True
Explanation:
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.
True
Explanation: