- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – How to Extract all the digits from a String
When it is required to extract strings with a digit, a list comprehension and ‘isdigit’ method is used.
Below is a demonstration of the same −
my_string = "python is 12 fun 2 learn" print("The string is : ") print(my_string) my_result = [int(i) for i in my_string.split() if i.isdigit()] print("The numbers list is :") print(my_result)
The string is : python is 12 fun 2 learn The numbers list is : [12, 2]
A string is defined and is displayed on the console.
A list comprehension is used to iterate over the string, and every element is checked to see if it is a digit using the ‘isdigit’ function and is converted to an integer.
These are stored in a list and assigned to a variable.
This is the output that is displayed on the console.
- Python – Extract Rear K digits from Numbers
- Python – Extract Percentages from String
- Python – Extract String elements from Mixed Matrix
- Extract digits from Tuple list Python
- How to extract numbers from a string in Python?
- How to extract date from a string in Python?
- How to extract numbers from a string using Python?
- Get all digits from a string in Java
- Extract all integers from string in C++
- How to extract a substring from inside a string in Python?
- How to extract all the .txt files from a zip file using Python?
- How to extract data from a string with Python Regular Expressions?
- Python – Extract range of Consecutive similar elements ranges from string list
- Extract decimal numbers from a string in Python
- Python – Extract elements from Ranges in List