How to check if a string has at least one letter and one number in Python?



In this article, we are going to find out how to check if a string has at least one letter and one number in Python.

Regular expressions is used in the first technique. Import the re library and install it if it isn't already installed to use it. After importing the re library, we can use the regular expression ('^(?=.*[0-9]$)(?=.*[a-zA-Z])'. This will return False if the string contains any special characters other than Alphabets and Numbers; otherwise, True will be returned.

In regular expressions, the ?= syntax is used to call lookaheads. Lookaheads discover matches in the provided string by looking ahead in the string from the present place.

Example 1

In the example given below, we took a string as an input and we are finding out if the string has at least one alphabet and one number using regular expressions 

import re

str1 = "Tutorialspoint@123"
print("The given string is ")
print(str1)

res = bool(re.match('^(?=.*[0-9]$)(?=.*[a-zA-Z])', str1))
print("Checking whether the given string contains at least one alphabet and one number")
print(res)

Output

The output of the above example is given below −

The given string is 
Tutorialspoint@123
Checking whether the given string contains at least one alphabet and one number
True

Example 2

In the example given below, we are taking the same program as above but we are sending a different string as input 

import re

str1 = "Tutorialspoint!@#"
print("The given string is ")
print(str1)

res = bool(re.match('^(?=.*[0-9]$)(?=.*[a-zA-Z])', str1))
print("Checking whether the given string contains at least one alphabet and one number")
print(res)

Output

Following is an output of the above code −

The given string is 
Tutorialspoint!@#
Checking whether the given string contains at least one alphabet and one number
False

Using isalpha() method and isdigit() method

The second way is to examine each letter individually to determine whether it is an alphabet, a number, or anything else. We'll use isalpha() method to check alphabets and isdigit() method to check numbers in this technique.

Example 1

In the program given below, we are taking a string as input and iterating it over, and checking if there is at least one alphabet and one number 

def checkString(str1):
   letter_flag = False
   number_flag = False
   for i in str1:
      if i.isalpha():
         letter_flag = True
      if i.isdigit():
         number_flag = True
      return letter_flag and number_flag
      
str1 = "Tutorialspoint123"
print("The given string is ")
print(str1)

res = checkString(str1)
print("Checking whether the given string contains at least one alphabet and one number")
print(res)

Output

The output of the above example is given below −

The given string is 
Tutorialspoint123
Checking whether the given string contains at least one alphabet and one number
False

Example 2

In the example given below, we are taking the program the same as above but we are sending another string as input and checking if it has at least one alphabet and one number −

def checkString(str1):
   letter_flag = False
   number_flag = False
   for i in str1:
      if i.isalpha():
         letter_flag = True
      if i.isdigit():
         number_flag = True
      return letter_flag and number_flag
        
str1 = "Tutorialspoint!@#"
print("The given string is ")
print(str1)

res = checkString(str1)
print("Checking whether the given string contains at least one alphabet and one number")
print(res)

Output

The output of the following program is 

The given string is Tutorialspoint!@#
Checking whether the given string contains at least one alphabet and one number
False

Advertisements