
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding Words with Both Alphabet and Numbers Using Python
Python refers to the high-level programming language that has various tools and built-in functions to analyze text data. This problem statement is a common task that uses some built-in functions- filter(), isdigit(), isalpha(), findall(), split(), and, append() to find the both alphabet and numbers using Python.
Let's see how to take the input of the program to get the result:
Input
my_text = "WELCOME TO CLUB100"
Output
CLUB100
Syntax
The following syntax is used in the examples-
filter()
The filter() method is applied when it filter the items based on specific conditions. In simple terms, it allows users to iterate extracted elements to satisfy the condition.
isdigit()
The isdigit() is the built-in method in Python that returns true if the character found digit otherwise false.
isalpha()
This built-in function returns true if all the characters are alphabet letters from a-z.
re.findall()
The findall() method follows the module named re to return a list of strings. The re represents regular expressions that attempt to match the pattern from input text.
split()
The split() is a built-in method in Python that breaks the list of words into a string.
append()
The append() function is used to add the text element to the end of the list.
Using Regular Expression
The program uses the regular expression symbol in the respective variable and finds both alphabets using the built-in function findall().
Example
In the following example, begin the program by defining the function named find_alphabet_num that accepts the text string as a parameter. Then function uses a regular expression to find the pattern match of the text that includes both alphabets and numbers. Next, it will use the findall() method that follows the re module to find all occurrences of the pattern text.
import re def find_alphabets_num(text): pattern = r'\b(?=\w*\d)(?=\w*[a-zA-Z])\w+\b' words = re.findall(pattern, text) return words # create the string my_text = "This is RX100" # function call result = find_alphabets_num(my_text) print(result)
Output
['RX100']
Using list Comprehension and Split Method
The program uses split() to separate the individual words and list comprehension to include various built-in functions- isdigit(), isalpha(), and, any() to iterate through a loop.
Example
In the following example, the function find_alphabet_num take takes an input string. Then the function split separates the input text into words to contain only those that include the alphabet and number. Next, use the function return to returning the words as a list. Then create the string to store in the variable my_text. The function call will use in the variable result to pass the parameter i.e. my_text. Finally, we are printing the output with the help of variable result.
def find_alphabet_num(text): words = text.split() filtered_words = [word for word in words if any(c.isdigit() for c in word) and any(c.isalpha() for c in word)] return filtered_words # Create the string my_text = "Microsoft365, MS Word, MS Excel" # function call result = find_alphabet_num(my_text) print(result)
Output
['Microsoft365,']
Using a loop and String Method
The program uses the for loop to iterate into the input string by using a recursive function, split(), any(), isalpha(), and, conditional statements.
Example
In the following example, start the program by defining the function named find_words_alphabet that accepts the parameter name text( receive the input string value). Then the split() method separates the word and checks whether the word contains both strings as input. Next, use the three simple steps- create the string, function call, and, print function to get the desired result.
def find_words_alphabets_num(text): words = text.split() filtered_words = [] for word in words: if any(c.isdigit() for c in word) and any(c.isalpha() for c in word): filtered_words.append(word) return filtered_words # Create the string my_text = "Microsoft365, MS Word, MS Excel" # function call result = find_words_alphabets_num(my_text) print(result)
Output
['Microsoft365,']
Using a Lambda Function and filter()
The program uses the filter() to remove those characters with numbers in its string. The lambda function calculates the result of both alphabets and numbers by using for loop, and built-in functions[any() and isdigit()].
Example
In the following example, the function find_alphabet_number accepts the parameter text to receive the value of the string through a function call. Then it will use the method name split() to separate all the words including digits and alphabet by using the built-in method filter() and lambda and store it in the variable words. Next, it returns the list using function return. Moving ahead to create the input string in the variable my_text. Then use the function call in the variable result and use the same variable in the print function to get the result.
def find_alphabet_number(text): words = text.split() filtered_words = filter(lambda word: any(c.isdigit() for c in word) and any(c.isalpha() for c in word), words) return list(filtered_words) # Create the string my_text = "My World123" result = find_alphabet_number(my_text) print(result)
Output
['World123']
Conclusion
We discussed the various ways to solve the finding words with both alphabet and numbers. For example- regular expressions help to set the pattern match of the input text which contains the alphabet along with its number whereas in another way other built-in functions also solve the same problem.