DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Find Numbers in a String Using Python

Use Python’s re.findall(r'd+', text) to extract digit runs as strings. Choose a pattern carefully for signs, decimals, ASCII-only input, or match positions.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To extract runs of decimal digits from text, use Python’s regular-expression module: re.findall(r'd+', text). It returns matches as strings, so choose a conversion such as int() only after deciding what formats your input can contain.

Extract digit sequences with re.findall()

For ordinary runs of digits, d+ means “one or more consecutive decimal digits.” With no capturing groups, re.findall() returns each full match in a list:

As an Amazon Associate I earn from qualifying purchases.

import re

text = "Order 17 contains 3 items"
numbers = re.findall(r"d+", text)
print(numbers)  # ['17', '3']

The result contains strings, not numeric values. Use a raw string such as r"d+" for the pattern so Python handles the backslash as part of the regular expression. See Python’s regular-expression documentation.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a pattern that matches your number format

A regex finds the character sequences its pattern describes; it does not infer every possible way a number might be written. Decide which formats are valid in your input before extracting or converting matches.

Need Example pattern or method What it handles
Unsigned digit runs r'd+' One or more consecutive Unicode decimal digits in a Python str pattern.
ASCII digits only r'[0-9]+' One or more characters from ASCII 0 through 9.
Optional sign and decimal fraction r'[+-]?d+(?:.d+)?' An optional plus or minus, digits, and an optional period followed by digits. It does not include exponents, grouped digits, or forms such as .5.
Match locations as well as text re.finditer(pattern, text) Each match’s value and its start and end positions.

The signed-decimal pattern is only an example grammar. For example, it will match the digits in 1e3 separately rather than treating the text as an exponent-form number. Extend or replace the pattern to fit the notation your data actually uses; locale-specific separators and digit grouping also need explicit handling.

Get match positions with re.finditer()

When you need to know where each match occurs—for example, to highlight it in the original text—iterate over match objects rather than collecting strings alone:

import re

text = "Order 17 contains 3 items"
for match in re.finditer(r"d+", text):
    print(match.group(), match.start(), match.end())

group() returns the matched text; start() and end() give its character positions in the input string. Use findall() when the matched strings are all you need.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand what findall() returns with groups

With no capturing group, findall() returns full matches. If the pattern has one capturing group, it returns that group’s text; with multiple capturing groups, it returns tuples. If parentheses are needed to group part of a pattern without changing the result shape, use a non-capturing group such as (?:.d+)?.

Decide how Unicode digits should behave

In a Unicode str regex, d matches Unicode decimal digits, not only ASCII 0–9. If your input must use ASCII digits, write [0-9] or use the re.ASCII flag.

Python’s string tests describe different character classes: isdecimal() recognizes decimal characters, isdigit() also accepts certain additional characters such as superscript digits, and isnumeric() is broader still. A character accepted by isdigit() is not necessarily suitable for an ordinary base-10 numeral. These methods test a string as a whole; they do not scan prose for embedded values. See the built-in types documentation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Convert extracted strings only when appropriate

Use int(value) for matched strings that follow the integer syntax it accepts. Use float(value) only when the text follows Python’s accepted float syntax. A regex can deliberately allow text that a chosen conversion function rejects, so handle conversion errors when that is possible.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
values = re.findall(r"d+", "Order 17 contains 3 items")
integers = [int(value) for value in values]
# [17, 3]

For data with signs, decimals, exponents, or separators, make the extraction pattern and conversion step agree on the same grammar. If numbers are tokens within a larger structured language rather than loose text, Python’s regex documentation shows a named NUMBER pattern used as part of tokenization.

Check the pattern against your input

  • Choose whether you want digit runs or complete signed and decimal numbers.
  • Test representative cases, including punctuation, adjacent text, and input with no match.
  • Decide whether Unicode decimal digits or ASCII digits are valid.
  • Use finditer() if match positions matter; otherwise use findall().
  • Confirm that the conversion function accepts every matched form, and handle failures where necessary.

The examples use standard-library interfaces documented for Python 3.14.8 (re) and Python 3.14.7 (built-in types). Check the documentation for the Python version installed in your environment.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.