Extract Integer from String in Python

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,

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

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.

Output:

287

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.

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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.