
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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']
- Related Articles
- Decrypt String from Alphabet to Integer Mapping in Python
- How to get a string from a tkinter filedialog in Python 3?
- How can we get substring from a string in Python?
- Get unique values from a list in Python
- How to convert an integer to a hexadecimal string in Python?
- How to convert an integer to a octal string in Python?
- How to get a list of all the values from a Python dictionary?
- How to get min alphabetical character from the string in Python?
- Python Pandas - Get unique values from a column
- How to convert String to Integer and Integer to String in Java?
- How to Get the Sign of an Integer in Python?
- How to get max alphabetical character from the string in Python?\n
- How to get parameters from a URL string in PHP?
- How to get the length of a string in Python?
- How to get the size of a string in Python?
