October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Find the First Number in a String in Python

Find the first contiguous digit run in a Python string with re.search, safely handle no match, and decide whether you need Unicode digits, ASCII digits, or a complete number format.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Use re.search(r"d+", text) to find the first contiguous run of decimal digits anywhere in a Python string. Check whether it returned None before calling group(); convert the matched text with int() only if you need a numeric value.

Find the first digit run with re.search

re.search scans through a string and returns a match object for the first place the pattern matches. If there is no match, it returns None. The match’s group() method returns the matched text.

import re

text = "Order 27 ships after 4 days"
match = re.search(r"d+", text)

if match is None:
    first_number = None
else:
    first_number = match.group()

print(first_number)  # 27

Here, first_number is the string "27". If you need an integer instead, convert it after checking for a match:

first_number = int(match.group()) if match is not None else None

Why use search instead of match?

re.match checks for a match at the beginning of the string. re.search scans forward, so it can find digits after words or punctuation. For example, with "Order 27", re.search(r"d+", text) finds "27", while re.match does not match the digits at the start.

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

See the Python 3.14.8 regular-expression documentation for the behavior of search, match, and match results.

Choose which digits count

Unicode decimal digits

In a pattern applied to a Python str, d matches Unicode decimal digits, not only the ASCII characters 0 through 9. Use it when that broader decimal-digit behavior is appropriate.

ASCII digits only

To restrict the match to the characters 0–9, use an explicit character class:

match = re.search(r"[0-9]+", text)

You can also keep d and set the ASCII flag:

match = re.search(r"d+", text, flags=re.ASCII)

Decide what “number” means for your input

The pattern d+ returns a contiguous run of digits. It does not include a preceding sign, decimal point, or exponent as part of the match. For example, it finds the digit runs in text such as "-12.5e3"; it does not parse that whole expression as one number.

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

If the input can contain signed integers or floating-point values, define that grammar first and use a pattern that matches it. The Python regular-expression reference includes examples for signed integers and floating-point numbers.

Get the match position or find every run

Get the location

Keep the match object if you need the character index or range as well as the text. Use start() for the starting index or span() for the start and end indices:

if match is not None:
    print(match.group())  # matched text
    print(match.start())  # starting index
    print(match.span())   # (start, end)

Find all digit runs

Use re.findall to get a list of matching strings, or re.finditer to get match objects with positions:

runs = re.findall(r"d+", text)
matches = list(re.finditer(r"d+", text))

The CPython Regular Expression HOWTO explains these extraction options and match-object use.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why str.isdigit() is not a substring search

str.isdigit() answers whether a whole non-empty string consists only of characters classified as digits. It does not locate the first digit run inside a larger string. Its character set is also broader than the Unicode decimal digits matched by a regex d. See the Python built-in types documentation for its precise definition.

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
Windows Errors? Fix Them Before They SpreadFree repair scan

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.