Python – Find Words with both alphabets and numbers
Sometimes, while working with Python strings, we can have problem in which we need to extract certain words with contain both numbers and alphabets. This kind of problem can occur in many domains like school programming and web-development. Lets discuss certain ways in which this task can be performed. Method #1 : Using any() + isdigit() + isalpha() The combination of above functionalities can be used to perform this task. In this, we iterate for all the words and check for required combination using isdigit() and isalpha().
Python3
# Python3 code to demonstrate working of# Words with both alphabets and numbers# Using isdigit() + isalpha() + any()# initializing stringtest_str = 'geeksfor23geeks is best45 for gee34ks and cs'# printing original stringprint("The original string is : " + test_str)# Words with both alphabets and numbers# Using isdigit() + isalpha() + any()res = []temp = test_str.split()for idx in temp: if any(chr.isalpha() for chr in idx) and any(chr.isdigit() for chr in idx): res.append(idx) # printing resultprint("Words with alphabets and numbers : " + str(res)) |
The original string is : geeksfor23geeks is best45 for gee34ks and cs Words with alphabets and numbers : ['geeksfor23geeks', 'best45', 'gee34ks']
Method #2 : Using regex This is yet another way by which we can perform this task. In this, we feed the string to findall(), and extract the required result. Returns strings till the numbers only.
Python3
# Python3 code to demonstrate working of# Words with both alphabets and numbers# Using regeximport re# initializing stringtest_str = 'geeksfor23geeks is best45 for gee34ks and cs'# printing original stringprint("The original string is : " + test_str)# Words with both alphabets and numbers# Using regexres = re.findall(r'(?:\d+[a-zA-Z]+|[a-zA-Z]+\d+)', test_str) # printing resultprint("Words with alphabets and numbers : " + str(res)) |
The original string is : geeksfor23geeks is best45 for gee34ks and cs Words with alphabets and numbers : ['geeksfor23', 'best45', 'gee34']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method #3: Using operator.countOf() method.
Python3
# Python3 code to demonstrate working of# Words with both alphabets and numbersimport operator as op# define all digitsall_digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']# define all lettersall_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']# initializing stringtest_str = 'geeksfor23geeks is best45 for gee34ks and cs'# printing original stringprint("The original string is : " + test_str)# Words with both alphabets and numbers# Using isdigit() + isalpha() + any()res = []temp = test_str.split()for idx in temp: if any(op.countOf(all_letters, chr) > 0 for chr in idx) and any(op.countOf(all_digits, chr) > 0 for chr in idx): res.append(idx)# printing resultprint("Words with alphabets and numbers : " + str(res)) |
The original string is : geeksfor23geeks is best45 for gee34ks and cs Words with alphabets and numbers : ['geeksfor23geeks', 'best45', 'gee34ks']
Time Complexity: O(n)
Space Complexity: O(n)






Please Login to comment...