HOWTO · Python
How to Extract Numbers From a String in Python
Learn how to extract digit runs, whitespace-delimited integers, and signed decimal values from Python strings with regular expressions and string methods.
On this page
To extract numbers from a Python string, choose whether you need each run of digits, whole whitespace-delimited integer tokens, or signed decimal values. These approaches return different types and recognize different formats, so match the method to the text you are parsing.
Extract digit runs with regular expressions
Use re.findall() with a digit-run pattern to collect each consecutive run of ASCII digits. The matches are strings, which is useful when you want to preserve the original text or process the values later.
Run this example:
import re
text = "24 apples 7 pears reading -5.5 adjustment +12.0"
digit_runs = re.findall(r"[0-9]+", text)
whole_tokens = [int(token) for token in text.split() if token.isdecimal()]
signed_decimals = re.findall(
r"(?<![\w.+-])[+-]?[0-9]+(?:\.[0-9]+)?(?![\w.+-])",
text,
)
print("Digit runs:", digit_runs)
print("Whole tokens:", whole_tokens)
print("Signed/decimal tokens:", signed_decimals)
Digit runs: ['24', '7', '5', '5', '12', '0']
Whole tokens: [24, 7]
Signed/decimal tokens: ['24', '7', '-5.5', '+12.0']
re.findall() is a function in the re module. [0-9]+ matches one or more adjacent ASCII digits wherever they occur, so -5.5 becomes two digit runs, 5 and 5. Convert a matched string with int() only when you need an integer value; conversion is a separate step from finding the text.
Extract whole numbers with string methods
For text where each integer is separated from other content by whitespace, use split() and keep tokens for which str.isdecimal() returns true. This accepts strings made entirely of Unicode decimal digits (the decimal-digit characters that int() can convert), while excluding other digit-like characters such as superscript ². Convert the accepted tokens with int() when numeric values are needed. The example above returns [24, 7], a list of integers.
This method checks complete whitespace-delimited tokens. It skips a token such as A12 because the letters are part of that token, and skips -5 or 5.5 because the sign or decimal point makes the whole token fail isdecimal(). It does not group digits found inside words. If numbers may touch punctuation or other text, use a regular expression instead.
A character-by-character check using str.isdigit() can find digit characters, but it does not group a sequence such as 305 into one value. It would produce individual characters, so it is not a replacement for extracting digit runs.
Extract signed and decimal numbers
The final pattern in the example matches an optional + or -, then digits, and optionally a period followed by more digits. Its lookarounds require the whole number token to be separated from letters, digits, periods, and other signs; this prevents a rejected sign from being dropped while matching the digits after it. Thus -5 and +12.0 match when they are separate tokens, while x-5 is ignored because the number is attached to a word, and 1-2 is ignored because this pattern treats a hyphen between digits as part of one token rather than as a separator. The result remains a list of strings; use float() for decimal values or int() for integer values after matching.
This pattern assumes a period as the decimal separator and does not handle exponent notation such as 1e6, thousands separators such as 1,200, or locale-specific numeric formats. It also requires digits before and after a decimal point, so forms such as .5 and 5. need a pattern adapted to your input rules. Because adjacent signs and digits are treated as one token, inputs such as ranges or identifiers containing + or - are skipped where the token boundaries do not fit this rule; adapt the pattern if your input syntax defines different separators.
Choose a method and handle edge cases
Use [0-9]+ with re.findall() when you want digit runs regardless of nearby letters or punctuation. Use split() with str.isdecimal() when the input is already organized as whitespace-separated unsigned integers. Use a more specific regular expression when signs or decimal points matter, then convert the returned strings to numeric types only after checking that the pattern matches your data format.
Python has no single built-in operation that can infer every numeric convention in arbitrary text. For large text, apply the chosen pattern to each input chunk or line rather than building a second copy of the whole source when memory matters. For user input validation, extraction alone is not validation: decide which complete formats are allowed and reject or handle unmatched input according to the surrounding application.