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
Python Program to Extract Strings with a digit
When working with lists of strings, you often need to extract strings that contain at least one digit. Python provides several approaches to accomplish this task efficiently.
Methods Overview
The following are the various methods to accomplish this task ?
Using list comprehension, any() and isdigit() functions
Using any(), filter() and lambda functions
Without using any built-in functions
Using ord() function
Problem Example
Assume we have taken an input list containing strings. We will now extract strings containing a digit using the above methods ?
Input
string_list = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
Expected Output
List of Strings containing any digits: ['hello562', 'python20', '100']
In the above input list, the elements 'hello562', 'python20', '100' contain digits. Hence they are extracted from the input list.
Method 1: Using List Comprehension with any() and isdigit()
This approach uses list comprehension to traverse through input list elements and check whether the string contains any digit character by character using any() function and isdigit() function ?
# input list of strings
string_list = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
print("Input list:", string_list)
# traversing through list elements and checking if the string contains any digit
output_list = [item for item in string_list if any(c.isdigit() for c in item)]
print("List of Strings containing any digits:")
print(output_list)
Input list: ['hello562', 'tutorialspoint', 'python20', 'codes', '100'] List of Strings containing any digits: ['hello562', 'python20', '100']
Method 2: Using filter() with Lambda Function
The filter() function filters the specified sequence using a function that determines if each element should be included. A lambda function is an anonymous function with unlimited arguments but only one expression ?
# input list of strings
string_list = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
print("Input list:", string_list)
# filtering the list elements if any string character is a digit
output_list = list(filter(lambda s: any(e.isdigit() for e in s), string_list))
print("List of Strings containing any digits:")
print(output_list)
Input list: ['hello562', 'tutorialspoint', 'python20', 'codes', '100'] List of Strings containing any digits: ['hello562', 'python20', '100']
Method 3: Without Using Built-in Functions
This approach manually checks for digits by replacing each digit (0-9) with an empty string and comparing the lengths ?
# function that checks if string contains digits
def contains_digit(input_string):
digits_str = "0123456789"
temp = input_string
# replace each digit with empty string
for digit in digits_str:
input_string = input_string.replace(digit, "")
# if lengths differ, original string had digits
return len(temp) != len(input_string)
# input list of strings
string_list = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
print("Input list:", string_list)
# empty list for storing results
output_list = []
# check each element
for element in string_list:
if contains_digit(element):
output_list.append(element)
print("List of Strings containing any digits:")
print(output_list)
Input list: ['hello562', 'tutorialspoint', 'python20', 'codes', '100'] List of Strings containing any digits: ['hello562', 'python20', '100']
Method 4: Using ord() Function
The ord() function returns the Unicode code of a given character. We check if the character's Unicode value falls within the digit range (48-57) ?
# input list of strings
string_list = ['hello562', 'tutorialspoint', 'python20', 'codes', '100']
print("Input list:", string_list)
# empty list for storing results
output_list = []
# traverse each element
for element in string_list:
# check each character
for char in element:
# check if ASCII value is between 48-57 (digits 0-9)
if ord(char) >= ord('0') and ord(char) <= ord('9'):
output_list.append(element)
break # found digit, move to next element
print("List of Strings containing any digits:")
print(output_list)
Input list: ['hello562', 'tutorialspoint', 'python20', 'codes', '100'] List of Strings containing any digits: ['hello562', 'python20', '100']
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| List Comprehension + any() | High | Fast | Most scenarios |
| filter() + Lambda | Medium | Fast | Functional programming style |
| Manual (replace) | Low | Slow | Understanding concepts |
| ord() function | Medium | Medium | Unicode-aware processing |
Conclusion
For extracting strings containing digits, use list comprehension with any() and isdigit() for the best balance of readability and performance. The filter() approach works well for functional programming styles, while manual methods help understand the underlying logic.
