Extract Numbers From String Python

Here, we will develop a program to extract numbers from string in python. If the string has “There are 5 pens for 2 students” then the extract numbers will be [5, 2]. We will also develop how to extract digits from string in Python. If the string has “kn4ow5pro8am2” then the extract numbers will be 4582. We are using List comprehension, isdigit(), str.split(), join(), filter() methods and Regular Expression.

Get Number from String Python

We will take a string while declaring the variable. The For Loop extract number from string using str.split() and isdigit() function. The split function to convert string to list and isdigit function helps to get the digit out of a string. If so, use int(x) with the word as x to convert it to an integer. Then call list.append(object) with the integer as the object to append it to an output list.

# Python program to extract number from string

# take string
string = "2 Java developer and 5 Python developer"

# print original string
print("The original string:", string)

# using str.split() + isdigit()
num = []
for i in string.split():
    if i.isdigit():
        num.append(int(i))

# print extract numbers
print("Extract Numbers:", num)

Output:-

The original string: 2 Java developer and 5 Python developer
Extract Numbers: [2, 5]

Extract Integer from String Python

This program method is similar to the above method, but rather a shorthand method. In this program we uses the list comprehension technique. The list comprehension which can help us iterating through the list.

# Python program to extract number from string

# take string
string = "2 Java developer and 5 Python developer"

# print original string
print("The original string:", string)

# using List comprehension + isdigit() +str.split() 
num = [int(i) for i in string.split() if i.isdigit()]

# print extract numbers
print("Extract Numbers:", num)

Output:-

The original string: 2 Java developer and 5 Python developer
Extract Numbers: [2, 5]

Note:- This program is not recognize floats, negative integers, or integers in hexadecimal format.

Find Number in String Python

In this program, we are using Regular Expression (RegEx module) to extract numbers from string in python. We can use the findall function to check for the numeric occurrences using matching regex string.

# Python program to extract number from string

# importing RegEx module
import re

# take string
string = "2 Java developer and 5 Python developer"

# print original string
print("The original string:", string)

# using re.findall() methods
temp = re.findall(r'\b\d+\b', string)
num = list(map(int, temp))

# print extract numbers
print("Extract Numbers:", num)

Output:-

The original string: 2 Java developer and 5 Python developer
Extract Numbers: [2, 5]

Extract Digits from String Python

The filter() function filters the digits detected in string by the isdigit() function and join() function performs the task of reconstruction of join function.

# Python program to extract digits from string

# take string
string = "kn4ow5pro8am2"

# print original string
print("The original string:", string)

# using join() + filter() + isdigit()
num = ''.join(filter(lambda i: i.isdigit(), string))

# print extract digits
print("Extract Digits:", num)

Output:-

The original string: kn4ow5pro8am2
Extract Digits: 4582

Python get int from String

In this program, we are using Regular Expression (RegEx module) to extract digits from strings. We can define the digit type requirement, using “\D”, and only digits are extracted from the string.

# Python program to extract digits from string

# importing RegEx module
import re

# take string
string = "kn4ow5pro8am2"

# print original string
print("The original string:", string)

# using re.sub() methods
num = re.sub("\D", "", string)

# print extract digits
print("Extract Digits:", num)

Output:-

The original string: kn4ow5pro8am2
Extract Digits: 4582

Also See:- Check if String Starts with Vowel

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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