Table of Contents
How to extract integer from string in Python?
A string can store a sequence of characters. These characters can be alphabets, numbers, special characters, and more.
We will discuss how to extract integer from string in Python.
Using the isdigit()
function to extract integer from string in Python.
In Python, the isdigit()
function is used to check whether all the characters in a given string are digits or not.
To extract integer from string in Python with this function, we will check each character in a string and store every digit in a new string. This new string now contains the integer from the string and can be type cast to an integer using the int()
function.
For example,
1 2 3 4 5 6 7 8 9 |
s1 = "java287blog" s = '' for i in s1: if(i.isdigit()): s += i n = int(s) print(n) |
Output:
In the above example,
- We iterate over a string using the
for
loop. - We use the
if
statement and check whether each character is a digit or not using theisdigit()
method. - If the character is a digit, it is added to an empty string initialized beforehand.
- We convert this string to an integer using the
int()
function.
Iterating over a string can be tedious and take a lot of time while working with long iterables. A quick way to use the isdigit()
function is by applying the list comprehension technique.
In list comprehension, we aim to create a list using for
loop in a single line of code. We will create a list of all the characters of the string that return True with the isdigit()
function.
Finally, we will merge the characters of the list to a single string using the join()
function and convert this string to an integer using the int()
method.
This logic is implemented below.
1 2 3 4 5 6 |
s1 = "java287blog" s = ''.join([a for a in s1 if a.isdigit()]) n = int(s) print(n) |
Output:
Further reading:
Using the filter()
function to extract integer from string in Python
This method uses the functions from the previous approach. The filter()
function in Python can be used to filter out elements from a sequence based on a given function.
We can use the isdigit()
function to filter out the digits, and combine them with the join()
function as done previously.
See the following code.
1 2 3 4 5 6 |
s1 = "java287blog" s = ''.join(filter(str.isdigit, s1)) n = int(s) print(n) |
Output:
Using the re.search()
function to extract integer from string in Python
Regular expressions are a common way to identify substrings and process them using small patterns. These regex patterns can identify parts from the string.
We use the re
module in Python to work with regular expressions in Python. With the re.search()
function, we can find the occurrences of parts of the string that match a given regex pattern.
We will create a regex pattern to extract integer from string in Python using this function.
See the code below.
1 2 3 4 5 6 |
import re s1 = "java287blog" n = int(re.search(r'\d+', s1).group()) print(n) |
In the above example, we create a regex pattern \d+
that matches all the integers from the string. The re.search()
function returns a Match
object so we use the group()
function to extract the results from this object. The int()
method typecasts the result to an integer.
The re.findall()
function from this module is also useful when we encounter multiple occurrences of integers. It returns the matches in a list.
Conclusion
In this article, we discussed how to extract integer from string in Python. In the first two methods, we use the isdigit()
function in different ways to extract integer from string in Python. We extract each digit individually and combine them. The quick methods with this function involved the use of list comprehension and the filter()
function. We also discussed how to use regex to extract integer from string in Python. We used the re.search()
function to find the integer based on the regex pattern.
That’s all about how to extract Integer from String in Python.