How do I check if a string has alphabets or numbers in Python?


To check if a string has alphabets or numbers in Python, we can use some built-in methods like isalpha() and isdigit().

isalpha() returns True if all characters in a string are alphabets (letters), and False otherwise.

isdigit() returns True if all characters in a string are digits (numbers), and False otherwise.

Here are two examples that demonstrate how to check if a string has alphabets or numbers:

To Check if a String has Alphabets or Numbers

Example

In this example, we first prompt the user to enter a string using the input() function. We then check if the string has only alphabets using the isalpha() method. If the string has only alphabets, we print "The string has only alphabets."

If the string doesn't have only alphabets, we check if it has only numbers using the isdigit() method. If the string has only numbers, we print "The string has only numbers."

If the string doesn't have only alphabets or only numbers, we print "The string has both alphabets and numbers."

input_str = input("Enter a string: ")

if input_str.isalpha():
    print("The string has only alphabets.")
elif input_str.isdigit():
    print("The string has only numbers.")
else:
    print("The string has both alphabets and numbers.")

Output

Enter a string: alphabet123
The string has both alphabets and numbers.

Example

In this example, we define a function called has_alpha_or_num() that takes a string as input and checks if it has both alphabets and numbers using a for loop and the isalpha() and isdigit() methods.

We then define a string called my_string that has both alphabets and numbers and call the has_alpha_or_num() function with my_string as input. Since my_string has both alphabets and numbers, the output will be "The string has both alphabets and numbers."

def has_alpha_or_num(input_str):
    has_alpha = False
    has_num = False
    for char in input_str:
        if char.isalpha():
            has_alpha = True
        elif char.isdigit():

            has_num = True
        if has_alpha and has_num:
            return True
    return False

my_string = "abc123"
if has_alpha_or_num(my_string):
    print("The string has both alphabets and numbers.")
else:
    print("The string does not have both alphabets and numbers.")

Output

The string has both alphabets and numbers.

Example

In this example, we use a for loop to iterate through each character in the input string and use the isalpha() and isdigit() methods to check if the character is an alphabet or a number.

We then use two Boolean variables, has_alpha and has_num, to keep track of whether the string has alphabets and numbers respectively. If both has_alpha and has_num are True, we know that the string has both alphabets and numbers and we print "The string has both alphabets and numbers."

If only has_alpha is True, we know that the string has only alphabets and we print "The string has only alphabets." If only has_num is True, we know that the string has only numbers and we print "The string has only numbers."

If neither has_alpha nor has_num is True, we know that the string has neither alphabets nor numbers and we print "The string has neither alphabets nor numbers."

input_str = input("Enter a string: ")
has_alpha = False
has_num = False

for char in input_str:
    if char.isalpha():
        has_alpha = True
    elif char.isdigit():
        has_num = True
    if has_alpha and has_num:
        break

if has_alpha and has_num:
    print("The string has both alphabets and numbers.")
elif has_alpha:
    print("The string has only alphabets.")
elif has_num:
    print("The string has only numbers.")
else:
    print("The string has neither alphabets nor numbers.")

Output

Enter a string: #$&*@#
The string has neither alphabets nor numbers.

Example

In this example, we use the re module to perform regular expression matching on the input string.

We use the regular expression [a-zA-Z] to match any alphabet (upper or lowercase) and \d to match any digit. We use the search() function of the re module to search for these patterns in the input string.

If both [a-zA-Z] and \d are found in the input string, we know that the string has both alphabets and numbers and we print "The string has both alphabets and numbers."

If only [a-zA-Z] is found, we know that the string has only alphabets and we print "The string has only alphabets." If only \d is found, we know that the string has only numbers and we print "The string has only numbers."

If neither [a-zA-Z] nor \d is found, we know that the string has neither alphabets nor numbers and we print "The string has neither alphabets nor numbers."

import re
input_str = input("Enter a string: ")
if re.search(r'[a-zA-Z]', input_str) and re.search(r'\d', input_str):
    print("The string has both alphabets and numbers.")
elif re.search(r'[a-zA-Z]', input_str):
    print("The string has only alphabets.")
elif re.search(r'\d', input_str):
    print("The string has only numbers.")
else:
    print("The string has neither alphabets nor numbers.")

Output

Enter a string: xyz123
The string has both alphabets and numbers.

Example

In this example, we use the any() function to check if any character in the input string satisfies the condition.

We use the isalpha() method to check if a character is an alphabet and the isdigit() method to check if a character is a digit. We pass a generator expression to the any() function to check if any character in the input string satisfies each condition.

If both an alphabet and a digit are found in the input string, we know that the string has both alphabets and numbers and we print "The string has both alphabets and numbers."

If only an alphabet is found, we know that the string has only alphabets and we print "The string has only alphabets." If only a digit is found, we know that the string has only numbers and we print "The string has only numbers."

If neither an alphabet nor a digit is found, we know that the string has neither alphabets nor numbers and we print "The string has neither alphabets nor numbers."

input_str = input("Enter a string: ")
if any(char.isalpha() for char in input_str) and any(char.isdigit() for char in input_str):
    print("The string has both alphabets and numbers.")
elif any(char.isalpha() for char in input_str):
    print("The string has only alphabets.")
elif any(char.isdigit() for char in input_str):
    print("The string has only numbers.")
else:
    print("The string has neither alphabets nor numbers.")

Output

Enter a string: 5678910
The string has only numbers.

Updated on: 10-Aug-2023

785 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements