Sling Academy
Home/Python/Python: How to Extract Numbers from Text (2 Approaches)

Python: How to Extract Numbers from Text (2 Approaches)

Last updated: June 04, 2023

This practical, example-based article will show you a few ways to extract numbers (integers and floats, positive and negative) from text in Python. There’s no time to waste; let’s get our hands dirty with code.

Using regular expressions

Regular expressions provide flexibility to extract numbers with different formats. However, defining a proper pattern might be tough, even with experienced programmers. Below is the regular expression pattern we’ll use in the example to come:

r"[-+]?(?:\d+(?:,\d\d\d)*(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?"

This pattern is designed to match a wide range of number formats, including positive or negative whole numbers, decimal numbers, and numbers with exponential notation. It also handles optional thousands separators (comma) and accounts for the presence of a decimal part (dot) or exponent.

Full example:

# slingacademy.com
import re

# Define a function to extract numbers from a string
def extract_numbers(text):
    pattern = r"[-+]?(?:\d+(?:,\d\d\d)*(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?"
    numbers = re.findall(pattern, text)
    return numbers

text = """123 is a positive integer. This year is 2023. 3.14 is a float number. Examples of a negative float numbers are -12.345 and -6.789. 
And here is a number with comma separators: 1,234,567.89.
"""

numbers = extract_numbers(text)
print(numbers)

Output:

['123', '2023.', '3.14', '-12.345', '-6.789', '1,234,567.89']

The result is a list of numeric strings. In case you need a list of floats, just do like this:

numbers = [float(number.replace(",", "")) for number in numbers]

Using string methods and operations

You can use list comprehension and split() to convert the text into a list of words, and then filter out the words that are digits using isdigit(). This approach is far simpler than the previous one, but the trade-off is that it can only handles simple use cases.

Code example:

text = "There are 6 turtles in the pond, and 3 of them are red-eared sliders."

numbers = [int(s) for s in text.split() if s.isdigit()]
print(numbers)

Output:

[6, 3]

Note that the code snippet above only works with positive integers. It will overlook negative numbers as well as floats and other number formats.

Next Article: Python: Get Numeric Value from User Input

Previous Article: Python: Check If a String Can Be Converted to a Number

Series: Python – Numbers & Math Tutorials

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots