How to get integer values from a string in Python?


In this article, we are going to find out how to get integer values from a string in Python.

The first approach is by using the filter() method. We will pass the string and the method isdigit() to the filter method. Python has a built-in function called Filter(). An iterable, like a list or dictionary, can have the filter function applied to it to create a new iterator. Based on the criteria you provide, this new iterator can filter out specific elements quite well.

The filter() method checks the string for digits and filters out the characters that satisfy the condition isdigit(). We need to convert the resulting output into int for getting integer output.

Example

In the example given below, we are taking a string as input and we are finding out the integers present in the string using the filter() and isdigit() method 

str1 = "There are 20 teams competing in the Premier League"

print("The given string is")
print(str1)

print("The number present in the string is")
print(int(filter(str.isdigit(), str1)))

Output

The output of the above example is as shown below−

The given string is
There are 20 teams competing in the Premier League
The number present in the string is
20

Using Regular Expressions

Regular expressions are used in the second technique. Import the re library and install it if it isn't already installed to use it. After importing the re library, we can use the regular expression "d+" to identify digits. The string and the Regular Expression "d+" will be sent as inputs to the re.findall() function, which will return a list of all the numbers contained in the provided string.

Example

In the example given below, we are taking a string as input and we are finding out the integers present in the string using Regular Expressions.

import re
str1 = "There are 21 oranges, 13 apples and 18 Bananas in the basket"

print("The given string is")
print(str1)

print("The number present in the string is")
print(list(map(int, re.findall('\d+', str1))))

Output

The output of the above example is given below −

The given string is
There are 21 oranges, 13 apples and 18 Bananas in the basket
The number present in the string is
[21, 13, 18]

Using split() method

The third approach is by using split(), append(), and isdigit() methods. Firstly, we will split the string at white spaces using the split() method, then we will check if each element is a digit or not using isdigit() method, if the element is digit, then by using the append() method we will add that element into a new list.

Example

In the example given below, we are taking a string as input and we are finding out the digits present in the string using split() method 

str1 = "There are 21 oranges, 13 apples and 18 Bananas in the basket"

print("The given string is")
print(str1)

print("The number present in the string is")
res = []
for i in str1.split():
   if i.isdigit():
      res.append(i)
print(res)

Output

The output of the above example is as shown below −

The given string is
There are 21 oranges, 13 apples and 18 Bananas in the basket
The number present in the string is
['21', '13', '18']

Updated on: 07-Dec-2022

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements