Python – Extract numbers from string



Python | Extract numbers from string

Many times, while working with strings we come across this issue in which we need to get all the numeric occurrences. This type of problem generally occurs in competitive programming and also in web development. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using List comprehension + isdigit() + split()
This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.

# Python3 code to demonstrate
# getting numbers from string 
# using List comprehension + isdigit() +split()
 
# initializing string 
test_string = "There are 2 apples for 4 persons"
 
# printing original string 
print("The original string : " + test_string)
 
# using List comprehension + isdigit() +split()
# getting numbers from string 
res = [int(i) for i in test_string.split() if i.isdigit()]
 
# print result
print("The numbers list is : " + str(res))

Output :

The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]

 

Method #2 : Using re.findall()
This particular problem can also be solved using python regex, we can use the findall function to check for the numeric occurrences using matching regex string.

 

# Python3 code to demonstrate
# getting numbers from string 
# using re.findall()
import re
 
# initializing string 
test_string = "There are 2 apples for 4 persons"
 
# printing original string 
print("The original string : " + test_string)
 
# using re.findall()
# getting numbers from string 
temp = re.findall(r'\d+', test_string)
res = list(map(int, temp))
 
# print result
print("The numbers list is : " + str(res))

Output :

The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]

 

Last Updated on October 27, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs