GeeksforGeeks App
Open App
Browser
Continue

# Python | Extract numbers from string

Many times, while working with strings we come across this issue in which we need to get all the numeric occurrences. This type of problem generally occurs in competitive programming and also in web development. Let’s discuss certain ways in which this problem can be solved.

Method #1: Using List comprehension + isdigit() + split() This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.

## Python3

 `# Python3 code to demonstrate``# getting numbers from string``# using List comprehension + isdigit() +split()` `# initializing string``test_string ``=` `"There are ``2` `apples ``for` `4` `persons"` `# printing original string``print``("The original string : " ``+` `test_string)` `# using List comprehension + isdigit() +split()``# getting numbers from string``res ``=` `[``int``(i) ``for` `i ``in` `test_string.split() ``if` `i.isdigit()]` `# print result``print``("The numbers ``list` `is` `: " ``+` `str``(res))`

Output :

```The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]```

Time Complexity: O(n), where n is the number of elements in the input string.
Auxiliary Space: O(n), where n is the number of numbers in the input string.

Method #2: Using re.findall() This particular problem can also be solved using python regex, we can use the findall function to check for the numeric occurrences using matching regex string.

## Python3

 `# Python3 code to demonstrate``# getting numbers from string``# using re.findall()``import` `re` `# initializing string``test_string ``=` `"There are ``2` `apples ``for` `4` `persons"` `# printing original string``print``("The original string : " ``+` `test_string)` `# using re.findall()``# getting numbers from string``temp ``=` `re.findall(r``'\d+'``, test_string)``res ``=` `list``(``map``(``int``, temp))` `# print result``print``("The numbers ``list` `is` `: " ``+` `str``(res))`

Output :

```The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]```

Method #3: Using isnumeric() method

## Python3

 `# Python3 code to demonstrate``# getting numbers from string` `# initializing string``test_string ``=` `"There are 2 apples for 4 persons"` `# printing original string``print``(``"The original string : "` `+` `test_string)`  `# getting numbers from string``res ``=` `[]``x``=``test_string.split()``for` `i ``in` `x:``    ``if` `i.isnumeric():``        ``res.append(``int``(i))` `# print result``print``(``"The numbers list is : "` `+` `str``(res))`

Output

```The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]```

Time Complexity : O(N)
Auxiliary Space : O(N)

Method #4 : Using Filter() function

Step-by-step algorithm for implementing the approach

1. Define the input string
2. Print the original string
3. Split the input string into a list of words using the split() method
4. Use the filter() function to filter out non-numeric elements from the list by applying the lambda function x.isdigit() to each element
5. Convert the remaining elements in the filtered list to integers using a list comprehension
6. Print the resulting list of integers

## Python3

 `# define the input string``test_string ``=` `"There are 2 apples for 4 persons"``print``(``"The original string : "` `+` `test_string)``# use the split() method to split the input string into a list of words``# use the filter() function to filter out non-numeric elements from the list``# use lambda function x.isdigit() to check if each element is a digit``res ``=` `list``(``filter``(``lambda` `x: x.isdigit(), test_string.split()))` `# use a list comprehension to convert the remaining elements to integers``res ``=` `[``int``(s) ``for` `s ``in` `res]` `# print the resulting list of integers``print``(``"The numbers list is : "` `+` `str``(res))`

Output

```The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]```

Time complexity: O(n), where n is the length of the input string. The split() method takes O(n) time to split the input string into a list of words, and the filter() function takes O(n) time to iterate over each element in the list and apply the lambda function. The list comprehension takes O(k) time, where k is the number of elements in the filtered list that are digits, and this is typically much smaller than n. Therefore, the overall time complexity is O(n).

Auxiliary space complexity: O(n), as the split() method creates a list of words that has the same length as the input string, and the filter() function creates a filtered list that can be up to the same length as the input list. The list comprehension creates a new list of integers that is typically much smaller than the input list, but the space complexity is still O(n) in the worst case. Therefore, the overall auxiliary space complexity is O(n)

Method #5: Using a loop and isdigit() method

Use a loop to iterate over each character in the string and check if it is a digit using the isdigit() method. If it is a digit, append it to a list.

## Python3

 `test_string ``=` `"There are 2 apples for 4 persons"` `numbers ``=` `[]``for` `char ``in` `test_string:``    ``if` `char.isdigit():``        ``numbers.append(``int``(char))` `print``(``"The numbers list is:"``, numbers)`

Output

`The numbers list is: [2, 4]`

Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(k), where k is the number of digits in the string.

Method #6: Using str.translate() with str.maketrans()

Step-by-step approach:

• Define the input string.
• Print the original string.
• Initialize a translation table to remove non-numeric characters using str.maketrans().
• Use str.translate() with the translation table to remove non-numeric characters from the string and store the result in a new string called numeric_string.
• Use str.split() to split the numeric_string into a list of words and store the result in a new list called words.
• Initialize an empty list called numbers to store the resulting integers.
• Iterate over each word in the list of words.
• Check if the word is a numeric string using str.isdigit().
• If the word is a numeric string, convert it to an integer using int() and append it to the list of numbers.
• Print the resulting list of integers.

Below is the implementation of the above approach:

## Python3

 `# Define the input string``test_string ``=` `"There are 2 apples for 4 persons"``# Print the original string``print``(``"The original string : "` `+` `test_string)``# Initialize a translation table to remove non-numeric characters``translation_table ``=` `str``.maketrans('``', '``', '``abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"``#\$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')``# Use str.translate() with the translation table to remove non-numeric characters``numeric_string ``=` `test_string.translate(translation_table)``# Use str.split() to split the string into a list of word``words ``=` `numeric_string.split()``numbers ``=` `[``int``(i) ``for` `i ``in` `words]``print``(``"The numbers list is : "` `+` `str``(numbers))`

Output

```The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]```

Time complexity: O(n), where n is the length of the input string. The str.translate() method and str.split() method take O(n) time, and iterating over each word in the list of words takes O(k) time, where k is the number of words in the list that are numeric strings.
Auxiliary Space: O(n), as we create a new string and a new list of words that each have the same length as the input string, and we create a new list of integers that has a maximum length of k, where k is the number of words in the list that are numeric strings.

Method #7: Using numpy:

1. Initialize the string test_string.
2. Split the string into a list of words using the split method.
3. Create a numpy array x from the resulting list.
4. Use np.char.isnumeric to create a boolean mask indicating which elements of x are numeric.
5. Use this boolean mask to index x and extract only the numeric elements.
6. Convert the resulting array of strings to an array of integers using astype.
7. Print the resulting array of integers.

## Python3

 `import` `numpy as np` `# initializing string``test_string ``=` `"There are 2 apples for 4 persons"` `# printing original string``print``(``"The original string : "` `+` `test_string)` `# getting numbers from string using numpy``x ``=` `np.array(test_string.split())``res ``=` `x[np.char.isnumeric(x)].astype(``int``)` `# print result``print``(``"The numbers list is : "` `+` `str``(res))``#This code is contributed by Vinay Pinjala.`

Output:

```The original string : There are 2 apples for 4 persons
The numbers list is : [2 4]```

Time complexity:  O(n), where n is the length of the original string test_string. This is because the split method takes O(n) time to split the string into a list of words, and the np.char.isnumeric method takes O(n) time to create the boolean mask. The remaining operations take constant time.

Auxiliary Space: O(n), where n is the length of the original string test_string. This is because we create a numpy array x to store the words of the string, which takes O(n) space. The space used by the resulting numpy array of integers is also O(n), since it contains all the numeric elements of the string.

My Personal Notes arrow_drop_up