Extract Number From String Python

Published On - 1 Apr 2024

In this article we will show you the solution of extract number from string python, in Python, we easily extract numbers from strings by using re. findall() method, filter() method, and numpy() module. re.

findall() represents the regular expression pattern it finds all the numbers present in the string. filter() method is filter find or remove the non-numeric characters in the string.

Within the filter() method, we pass lambda expression and use the isdigit() and isnumeric() methods to extract the numbers in a string and numpy module is used to extract numbers from strings in Python.

We use np.char.isdigit() to extract the digit in the list of strings. We first convert the string into a list of strings then apply np.

char.isdigit() operation to extract numbers from a string.

Step By Step Guide On Extract Number From String Python :-

1. Using re.findall() to extract number from string

import re
a="Tina is a good girl.She scored 98 marks out of 100"
y=[]
b=re.findall(r'\d+', a)
for i in b:
    y.append(int(i))
print(y) # [98, 100]
  1. Firstly, we can import re that represents regular expressions.
  2. We initialize a variable that represents the string.
  3. We create an empty list y which means after extracting the numbers in string we can append all the numbers in the list y.
  4. Then we can use re. findall () that extracts the numbers in the string and passes \d represents the digit by using this we easily extract numbers from the string.
  5. After that we can use for loop to iterate each element so that the string number is converted into an integer and append into list y.
#Using function
import re
def extract(a):
    y=[]
    b=re.findall(r'\d+', a)
    for i in b:
        y.append(int(i))
    return y
a=input()
print(extract(a))
  1. Firstly, we import re.
  2. We create a function named extract.
  3. We create an empty list y which means after extracting the numbers in string we can append all the numbers in the list y.
  4. We can use re. findall () that extracts the numbers in the string and pass \d that represents the digit by using this we easily extract numbers from the string.
  5. After that we can use for loop to iterate each element so that the string number is converted into an integer and append into list y.
  6. We initialize a variable that represents an input string.
  7. We call extract() function to give output.

2. Using filter() method to extract numbers from string

a="Rohan is 12 years old"
y=[]
b=a.split()
c=list(filter(lambda d: d.isdigit(), b))
for i in c:
    y.append(int(i))
print(y)
  1. We initialize a variable that represents the string.
  2. We create an empty list y which means after extracting the numbers in string we can append all the numbers in the list y.
  3. We convert the string into a list of strings.
  4. After that we use the filter() method to filter the non-numeric characters and use lambda expressions and use isdigit() method to extract numbers from strings.
  5. Then we can use for loop to iterate each element and append into y

3. Using numpy module

import numpy as np
a="Tina is a good girl and She scored 98 marks out of 100"
b=a.split()
c=np.array(b)
d=c[np.char.isdigit(c)].astype(int)
print(d)
  1. Firstly, we import the numpy module.
  2. We convert the string into a list of strings.
  3. Then we convert the list of strings into a numpy array.
  4. After that we use np.char.isdigit() that extracts the number from the string and astype(int) indicates that the number is converted into an integer. you can also use isnumeric() to extract the number from the string.

Conclusion :-

In conclusion, we successfully learned how to extract numbers from a string.

We used various methods that find numbers in the string and also implemented various code examples.

You can choose any one of the mentioned methods and code examples that suit you.

I hope this article on extract number from string python helps you and the steps and method mentioned above are easy to follow and implement.