Python Program to test if the String only Numbers and Alphabets
Last Updated :
06 Feb, 2023
Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations.
Examples:
Input : test_str = 'Geeks4Geeks'
Output : True
Explanation : Contains both number and alphabets.
Input : test_str = 'GeeksforGeeks'
Output : False
Explanation : Doesn't contain number.
Method #1 : Using isalpha() + isdigit() + any() + all() + isalnum()
In this, we check for all digits containing is alphabets and numbers combination using all(), isalpha() and isdigit(). The any() and isalnum() is used to filter out possibility of punctuations.
Python3
# Python3 code to demonstrate working of
# Test if string contains both Numbers and Alphabets only
# Using isalpha() + isdigit() + any() + all() + isalnum()
# initializing string
test_str = 'Geeks4Geeks'
# printing original string
print("The original string is : " + str(test_str))
# conditional combination for getting result.
res = not ((all(idx.isdigit() for idx in test_str) or (all(idx.isalpha()
for idx in test_str)) or (any(not idx.isalnum() for idx in test_str))))
# printing result
print("Does string contain both numbers and alphabets only? : " + str(res))
OutputThe original string is : Geeks4Geeks
Does string contain both numbers and alphabets only? : True
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #2 : Using regex
Using regex is one of the ways in which this problem can be solved.
Python3
# Python3 code to demonstrate working of
# Test if string contains both Numbers and Alphabets only
# Using regex
import re
# initializing string
test_str = 'Geeks4Geeks'
# printing original string
print("The original string is : " + str(test_str))
# conditional combination for getting result.
res = bool(re.match("^(?=.*[a-zA-Z])(?=.*[\d])[a-zA-Z\d]+$", "A530"))
# printing result
print("Does string contain both numbers and alphabets only? : " + str(res))
OutputThe original string is : Geeks4Geeks
Does string contain both numbers and alphabets only? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using in operator
Python3
# Python3 code to demonstrate working of
# Test if string contains both Numbers and Alphabets only
# initializing string
test_str = 'Geeks4Geeks'
lowercasealphabets="abcdefghijklmnopqrstuvwxyz"
uppercasealphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
digits="0123456789"
# printing original string
print("The original string is : " + str(test_str))
res=False
al=0
dig=0
for i in test_str:
if (i in lowercasealphabets) or (i in uppercasealphabets):
al+=1
elif (i in digits) :
dig+=1
if(al+dig==len(test_str)):
res=True
# printing result
print("Does string contain both numbers and alphabets only? : " + str(res))
#contributed by Bhavya Koganti
OutputThe original string is : Geeks4Geeks
Does string contain both numbers and alphabets only? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #4 : Using replace() and len() methods
Python3
# Python3 code to demonstrate working of
# Test if string contains both Numbers and
# alphabets only
# Initializing string
test_str = 'Geeks4Geeks'
lalphabets = "abcdefghijklmnopqrstuvwxyz"
ualphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
digits = "0123456789"
x = lalphabets+ualphabets+digits
# Printing original string
print("The original string is : " + str(test_str))
for i in x:
test_str = test_str.replace(i, "")
res = False
if(len(test_str) == 0):
res = True
# Printing result
print("Does string contain both numbers and alphabets only? : " + str(res))
OutputThe original string is : Geeks4Geeks
Does string contain both numbers and alphabets only? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using operator.countOf() method
Python3
# Python3 code to demonstrate working of
# Test if string contains both Numbers and Alphabets only
import operator as op
# initializing string
test_str = 'Geeks4Geeks'
lowercasealphabets="abcdefghijklmnopqrstuvwxyz"
uppercasealphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
digits="0123456789"
# printing original string
print("The original string is : " + str(test_str))
res=False
al=0
dig=0
for i in test_str:
if ( op.countOf(lowercasealphabets.split(),i)>0) or (op.countOf(uppercasealphabets.split(),i)>0):
al+=1
elif (op.countOf(digits.split(),i)>0) :
dig+=1
if(al+dig==len(test_str)):
res=True
# printing result
print("Does string contain both numbers and alphabets only? : " + str(res))
OutputThe original string is : Geeks4Geeks
Does string contain both numbers and alphabets only? : False
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #6:Using itertools.filterfalse() method
Python3
# Python3 code to demonstrate working of
# Test if string contains both Numbers and Alphabets only
import itertools
# initializing string
test_str = 'Geeks4Geeks'
# printing original string
print("The original string is : " + str(test_str))
# conditional combination for getting result.
res = list(itertools.filterfalse(
lambda x: (x.isdigit() or x.isalpha()), test_str))
if(res):
res = False
res = True
# printing result
print("Does string contain both numbers and alphabets only? : " + str(res))
OutputThe original string is : Geeks4Geeks
Does string contain both numbers and alphabets only? : True
Time Complexity: O(n)
Auxiliary Space: O(N)
Similar Reads
Python program to calculate the number of digits and letters in a string In this article, we will check various methods to calculate the number of digits and letters in a string. Using a for loop to remove empty strings from a list involves iterating through the list, checking if each string is not empty, and adding it to a new list.Pythons = "Hello123!" # Initialize cou
3 min read
Check Whether String Contains Only Numbers or Not - Python We are given a string s="12345" we need to check whether the string contains only number or not if the string contains only number we will return True or if the string does contains some other value then we will return False. This article will explore various techniques to check if a string contains
3 min read
Separate Alphabets and Numbers in a String - Python The task is to separate alphabets and numbers from a string. For example, given "a1b2c3", the output should be alphabets "abc" and numbers "123".Using List ComprehensionList comprehension offers a concise and efficient way to separate alphabets and numbers from a string. It iterates through each cha
2 min read
Python | Extract Strings with only Alphabets In Python, extracting strings that contain only alphabetic characters involves filtering out any strings that include numbers or special characters. The most efficient way to achieve this is by using the isalpha() method, which checks if all characters in a string are alphabetic.Pythonli= ['gfg', 'i
2 min read
Python - Differential Sort String Numbers and Alphabets Given a List String, Reorder List, with Sorted Alphabets followed by Sorted Strings. Input : test_list = ["1", "G", "10", "L", "9", "K", "4"] Output : ['G', 'K', 'L', '1', '4', '9', '10'] Explanation : Alphabets sorted, succeeded by sorted digits. Input : test_list = ["1", "G", "10", "L", "9"] Outpu
5 min read
Python Program to check whether all elements in a string list are numeric Given a list that contains only string elements the task here is to write a Python program to check if all of them are numeric or not. If all are numeric return True otherwise, return False. Input : test_list = ["434", "823", "98", "74"] Output : True Explanation : All Strings are digits.Input : tes
5 min read