Extract Integer from String in Python

[ad_1]

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.

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,

Output:

287

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 the isdigit() 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.

Output:

287

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.

Output:

287

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.

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.

[ad_2]

Leave a Comment