# How to extract integer from string in Python

To extract an integer from a string in Python, use the isdigit() function. The String isdigit() is a built-in Python method that returns True if all the characters are digits. Otherwise, False.

The string is a data type in Python that can have alphabets, numbers, and other special characters.

Strings are immutable in Python. Hence, we cannot add or modify data in the string. We can achieve that by creating a new string and storing the changed string. We can extract integers from strings by using the looping statements.

## Program for extracting integer from a string in Python

``````st = "45Hello6how3are4You5hello44world5"
num = []

for s in st:
if s.isdigit():
num.append(int(s))
print(num)``````

#### Output

``[4, 5, 6, 3, 4, 5, 4, 4, 5]``

In this program, we created a string that consists of both alphabets and numbers. Then, we created an empty list for storing numbers. Then we traverse over the string st.

If a character is a digit, we add that number to the num list. Hence, at last, all the numbers in the string are added to the list. Finally, this num list is printed to display all the numbers in the string.

The final output is a list containing only integers. We extracted the integers from a string in Python.

### Program for extracting integer from string using isdigit() function and list comprehension

List comprehension proposes a shorter syntax to create lists based on existing iterables in Python. First, we will create a list from a string where all the elements are integers.

``````st = "45Hello6how3are4You5hello44world5"
num = [int(s) for s in st if s.isdigit()]
print(num)``````

#### Output

``[4, 5, 6, 3, 4, 5, 4, 4, 5]``

This program is similar to the previous one. However, we used the list comprehension technique instead of for loop.

## Using regular expression

The findall() is a built-in re module function that returns a list containing all matches. The list comprises the matches in the order they are found. If no matches are found, an empty list is returned.

``````import re

st = "45Hello6how3are4You5hello44world5"
print("The string containing words and numbers is : " + st)
op = re.findall(r'\d', st)
nums = [int(i) for i in op]
print("The string containing numbers is : " + str(nums))
``````

#### Output

``````he string containing words and numbers is : 45Hello6how3are4You5hello44world5
The string containing numbers is : [4, 5, 6, 3, 4, 5, 4, 4, 5]``````

We used a module called regular expression (re) in this example. The re module is used for matching the patterns. First, we created a string that consists of alphabets and numbers. Then, we used the findall() function to extract all the integer data.

The “\d” in regular expression means digits. This is used to match the digits present in the string. Finally, the re.findall() function is used to find all the matching numbers in the string. Hence all the digits are extracted from the text.

Using list comprehension, we convert the list containing string elements to a list of integer elements.

## Use type casting to extract an integer from a string

The methodology of converting the value of one data type to another data type is called type conversion.

``````st = "45Hello6how3are4You5hello44world5"
integer = []
for s in st:
try:
if int(s):
integer.append(int(s))
except:
pass
print(integer)
``````

#### Output

``[4, 5, 6, 3, 4, 5, 4, 4, 5]``

We used the try and except block for handling errors in this program. The for loop gets every character from the string. This character is tried to convert into an int.

If an error is raised, the character is a string, so this will not be added to the list. However, if the try block didn’t throw an exception, then it means that the character is an integer, so the number is added to the list. It is the simplest way to extract the numbers from the string.

## Conclusion

You can use the list comprehension, isdigit(), for loop, re.findall() , and a combination of these functions to extract an integer from a string.

That’s it for this tutorial.

## Related posts

How to Filter List in Python

How to Generate a List of Numbers from 1 to N

How to Find Value in Array in Python