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.


import_contacts

You may also like:

Related Posts

  • 19 May

    Read CSV Line by Line in Python

    Table of ContentsRead CSV Line by Line Using the readline() Method in PythonRead CSV Line by Line Using the readlines() Method in PythonRead CSV Line by Line Using CSV Module in PythonRead CSV Line by Line Using the reader() MethodRead CSV Line by Line Using the DictReader() MethodConclusion CSV files or comma-separated files are used […]

  • 19 May

    Delete File if It Exists in Python

    Table of ContentsDelete File if Exists Using the os.remove() MethodDelete File if Exists Using the os.unlink() Method  Conclusion Python provides us with various tools to perform file handling operations. In this article, we will discuss different ways to delete file if it exists in the file system using python. Delete File if Exists Using the os.remove() […]

  • 19 May

    Convert Bytes to Int in Python

    Table of ContentsHow to Convert Bytes to Int in Python?How to Convert Bytes to Signed Int in Python?How to Convert Bytes to Unsigned Int in Python?Conclusion In this post, we will see how to convert Bytes to Int in Python. Normally, the data is stored in the storage in bytes format. In this article, we […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusion While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways to remove HTML […]

  • 19 May

    Get Directory Name From File Path in Python

    Table of ContentsHow to Get directory name from file path in python?Get Directory Name From File Path Using the os Module in PythonGet Directory Name From File Path Using the pathlib ModuleRemove FilnName from File PathRemove Filename From the File Path Using the OS Module in PythonRemove Filename From File Path Using the Pathlib Module […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.