Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding Words with both Alphabet and Numbers using Python
Finding words that contain both alphabetic characters and numeric digits is a common text processing task in Python. This tutorial demonstrates four different approaches using built-in functions like filter(), isdigit(), isalpha(), and regular expressions.
Problem Overview
Given a text string, we need to extract words that contain both letters and numbers ?
Input
my_text = "WELCOME TO CLUB100"
print("Input:", my_text)
Input: WELCOME TO CLUB100
Expected Output
['CLUB100']
Method 1: Using Regular Expressions
Regular expressions provide a powerful pattern-matching approach to find words containing both letters and digits ?
import re
def find_alphabets_num(text):
# Pattern: word boundary + contains digit + contains letter + word characters + word boundary
pattern = r'\b(?=\w*\d)(?=\w*[a-zA-Z])\w+\b'
words = re.findall(pattern, text)
return words
# Test the function
my_text = "This is RX100 and ABC test123"
result = find_alphabets_num(my_text)
print("Words with letters and numbers:", result)
Words with letters and numbers: ['RX100', 'test123']
Method 2: Using List Comprehension
List comprehension with any() function provides a concise Pythonic solution ?
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
# Test the function
my_text = "Microsoft365 MS Word Excel2019"
result = find_alphabet_num(my_text)
print("Filtered words:", result)
Filtered words: ['Microsoft365', 'Excel2019']
Method 3: Using For Loop
A traditional loop approach that checks each word individually ?
def find_words_alphabets_num(text):
words = text.split()
filtered_words = []
for word in words:
has_digit = any(c.isdigit() for c in word)
has_alpha = any(c.isalpha() for c in word)
if has_digit and has_alpha:
filtered_words.append(word)
return filtered_words
# Test the function
my_text = "Python3 is great for Data2024 analysis"
result = find_words_alphabets_num(my_text)
print("Result:", result)
Result: ['Python3', 'Data2024']
Method 4: Using Lambda Function and filter()
The filter() function with lambda creates a functional programming approach ?
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)
# Test the function
my_text = "My World123 and Code456"
result = find_alphabet_number(my_text)
print("Filtered result:", result)
Filtered result: ['World123', 'Code456']
Comparison
| Method | Performance | Readability | Best For |
|---|---|---|---|
| Regular Expression | Fast | Complex | Complex patterns |
| List Comprehension | Fast | High | Simple conditions |
| For Loop | Good | High | Step-by-step logic |
| Lambda + filter() | Good | Medium | Functional programming |
Conclusion
List comprehension offers the best balance of performance and readability for finding words with both letters and numbers. Use regular expressions for complex pattern matching and loops when you need step-by-step control over the filtering process.
