Skip to content
Home » How to Extract numbers from a String in Python

How to Extract numbers from a String in Python

In this tutorial let’s learn about How to Extract numbers from a String in Python. When working with strings, we frequently run into the problem of needing to get all of the numerical occurrences. This type of issue is common in competitive programming as well as online development. Let’s talk about some possible solutions to this situation.

Extract Numbers From a String Using List Comprehension Python

Simple list comprehension can be used to extract numbers from a string. The split() method is used to transform a string to a list of characters, while the isdigit() method is used to determine whether a digit is encountered during iteration.

This problem can be solved by utilizing the split function to convert a string to a list, then the list comprehension to iterate through the list, and the isdigit() function to extract a digit from a string.

Code:

#  string initialization
sample_string = "2 boys and 3 girls"
print("The Given string : " + sample_string)

# List comprehension
num = [int(i) for i in sample_string.split() if i.isdigit()]

# printing result
print("The numbers in given string are : " + str(num))

Output:

The Given string : 2 boys and 3 girls
The numbers in given string are : [2, 3]

Extract Numbers From a String Using re.findall() Module Python

Python’s re module also includes functions for searching through strings and extracting results. The findall() method in the re module delivers a list of all the matches. This problem may also be solved with python regex; we can use the findall function to look for numeric occurrences that match the regex string.

Code Example:

import re

#  string initialization
sample_string = "2 boys and 3 girls"
print("The Given string : " + sample_string)

# re.findall()
temp = re.findall(r'\d+', sample_string)
num = list(map(int, temp))

# printing result
print("The numbers in given string are : " + str(num))

Output:

The Given string : 2 boys and 3 girls
The numbers in given string are : [2, 3]

Extract Numbers From a String Using Numbers from String Library in Python

This is a fast hack if you don’t want to waste time typing specific code to extract numbers from a string. You can use the nums from string library to extract numbers from a given string by importing it. It has multiple regex rules with broad coverage and can be a valuable tool for NLP researchers.

Initially install the Numbers from string library using:

pip install nums_from_string

Code:

import nums_from_string

#  string initialization
sample_string = "2 boys and 3 girls"

print(nums_from_string.get_nums(sample_string))

Output:

[2, 3]

Conclusion

In this tutorial we learnt about different ways to extract the numbers from the given String. Python offers some packages like re and nums_from_String which are very helpful.

Similar Posts:

Remove Spaces From a String in Python
Convert a String to Variable Name in Python
Free Python Books List and Download Links