Open In App

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

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string in Python. The task is to check whether the string has at least one letter(character) and one number. Return “True” if the given string fully fill the above condition else return “False” (without quotes).

Examples: 

Input: welcome2ourcountry34
Output: True

Input: stringwithoutnum
Output: False

Approach: 

The approach is simple we will use loop and two flags for letter and number. These flags will check whether the string contains letter and number. In the end, we will take AND of both flags to check if both are true or not. Letters can be checked in Python String using the isalpha() method and numbers can be checked using the isdigit() method.

Python3




def checkString(str):
 
    # initializing flag variable
    flag_l = False
    flag_n = False
 
    # checking for letter and numbers in
    # given string
    for i in str:
 
        # if string has letter
        if i.isalpha():
            flag_l = True
 
        # if string has number
        if i.isdigit():
            flag_n = True
 
    # returning and of flag
    # for checking required condition
    return flag_l and flag_n
 
 
# driver code
print(checkString('thishasboth29'))
print(checkString('geeksforgeeks'))


Output

True
False

Time Complexity: O(n)
Auxiliary Space: O(n)

Approach: Without using builtin methods

Python3




def checkString(str):
 
    # initializing flag variable
    flag_l = False
    flag_n = False
     
    # checking for letter and numbers in
    # given string
    for i in str:
     
        # if string has letter
        if i in "abcdefghijklmnopqrstuvwxyz":
            flag_l = True
 
        # if string has number
        if i in "0123456789":
            flag_n = True
     
    # returning and of flag
    # for checking required condition
    return flag_l and flag_n
 
 
# driver code
print(checkString('thishasboth29'))
print(checkString('geeksforgeeks'))


Output

True
False

Time Complexity: O(n), where n is length of string.
Auxiliary Space: O(1)

Approach : Using operator.countOf() method

Python3




import operator as op
 
 
def checkString(str):
    letters = "abcdefghijklmnopqrstuvwxyz"
    digits = "0123456789"
 
    # initializing flag variable
    flag_l = False
    flag_n = False
 
    # checking for letter and numbers in
    # given string
    for i in str:
 
        # if string has letter
        if op.countOf(letters, i) > 0:
            flag_l = True
 
        # if string has digits
        if op.countOf(digits, i) > 0:
            flag_n = True
 
    # returning and of flag
    # for checking required condition
    return flag_l and flag_n
 
 
# driver code
print(checkString('thishasboth29'))
print(checkString('geeksforgeeks'))


Output

True
False

Time Complexity: O(N)
Auxiliary Space : O(1)

Approach : Using regular Expressions

Python3




import re
 
def checkString(str):
    # using regular expression to check if a string contains
    # at least one letter and one number
    match = re.search(r'[a-zA-Z]+', str) and re.search(r'[0-9]+', str)
    if match:
        return True
    else:
        return False
 
# driver code
print(checkString('thishasboth29'))
print(checkString('geeksforgeeks'))
#This code is contributed by Vinay Pinjala.


Output

True
False

Time Complexity: O(N)
Auxiliary Space : O(1)

Approach: Using  set intersection: 

 Algorithm:

1. Define a function “checkString” that takes a string “str” as input.
2. Create two sets – “letters” and “digits”, containing all the letters and digits respectively.
3. Check if the intersection of the input string and the sets of letters and digits is not empty using the ‘&’ operator.
4. Return True if both sets are not empty, otherwise return False.

Python3




def checkString(str):
    # Create sets of letters and digits
    letters = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
    digits = set('0123456789')
    # Check if the intersection of the input string and the sets of letters and digits is not empty
    return bool(letters & set(str)) and bool(digits & set(str))
# Test the function with two sample inputs
print(checkString('thishasboth29'))
print(checkString('geeksforgeeks'))
#This code is contributed by Jyothi pinjala.


Output

True
False

Time Complexity:

The time complexity of this algorithm is O(n), where n is the length of the input string. This is because the sets of letters and digits are constant and have a small size, so their creation can be considered as a constant time operation.
Space Complexity:

The space complexity of the algorithm is O(1), as we are only using two sets to store the letters and digits, which have a constant size. The input string is not modified and no extra space is used to store the output.

Approach: Using lambda function 

In this approach, we are using a lambda function to check if the given string contains both letters and numbers. We use the all function to check if all the characters in the string satisfy the given condition, which is that the character should either be an alphabet or a digit. We generate a boolean value for each character in the string using a generator expression inside the all function. Finally, we return the result of all function from the lambda function.

Python3




checkString = lambda s: any(c.isalpha() for c in s) and any(c.isdigit() for c in s)
 
# driver code
print(checkString('thishasboth29'))
print(checkString('geeksforgeeks'))


Output

True
False

Time Complexity: O(n)
Space Complexity: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads