Concepts / String-to-Number Conversion in Python

String-to-Number Conversion in Python

The complete process has three main stages: find matching lines and extract their values, convert the extracted strings to integers, and calculate the average.

  • Programming

From Revision Text to an Average

Formatted text often contains useful numbers mixed with labels. For example, several lines may use the format New Revision: followed by a revision number. The task is not simply to read the text: it is to identify the matching lines, extract the numerical parts, convert those extracted values into integers, and calculate an average.

Key point

The complete process has three main stages: find matching lines and extract their values, convert the extracted strings to integers, and calculate the average.

New Revision: 39772formatted text lineprefix plus captureddigitsregular expression39772, 39726strings returned byfindall()New Revision: 39726formatted text linematchmatchfindall() collects captured values
Which parts of each formatted line match the pattern, and what values does findall() return?

Capturing Digits with findall()

The input must be treated as a consistent format: the literal text New Revision: followed by a number. The regular expression therefore needs to recognize the fixed text and use a capturing group around the digits that follow it. The capturing group is important because it identifies the part to return rather than returning the entire formatted line.

Definition

findall() collects all values that match the regular expression. When the expression uses a capturing group for the numerical part, the collected results are the matched number strings rather than the complete labels and numbers.

Worked example

Extracting Revision Strings

Several text lines use the format New Revision: followed by a number. Extract the numerical parts with a regular expression containing a capturing group and findall().

  1. 1
    Identify the fixed format

    Each relevant line begins with the literal text New Revision: and then contains a number.

  2. 2
    Capture only the digits

    The regular expression should match the fixed prefix and isolate the numerical part with a capturing group.

  3. 3
    Collect every match

    findall() gathers the captured numerical parts from all matching lines.

Result

The result is a collection of number strings, such as 39772 and 39726, not integer values yet.

Why Conversion Comes Before Arithmetic

The values returned by findall() are strings. A string containing 39772 may look numeric to a person, but it remains text until it is explicitly converted with int(). The extracted values must become integers before they are used in mathematical operations.

StageValue formMeaning in the pipeline
After findall()number stringsText extracted from the formatted lines
After int()integersNumeric values ready for arithmetic

The same-looking digits have different roles before and after integer conversion.

42string from findall()int()explicit conversion42integer for arithmeticconvertinteger result
What changes when an extracted value such as 42 is converted before arithmetic?

Building the Average

Once every extracted string has been converted to an integer, the values can be processed mathematically. The average is calculated by summing all the values and dividing the sum by the count of values. The resulting average is then converted to an integer, as required by this task.

number stringsresults from findall()integer valuesafter int() conversionsumall integer values addedaveragesum divided by countinteger resultaverage converted to anintegerint()add valuesdivide by countconvert to integer
How do matched text values become one formatted average?
Worked example

Checking the Expected Average

Use the stated expected results to validate a completed extraction and calculation process for the two source files.

  1. 1
    Validate the full-file result

    For mbox.txt, compare the program's final integer average with the expected result of 38549.

  2. 2
    Validate the shorter-file result

    For mbox-short.txt, compare the program's final integer average with the expected result of 39756.

  3. 3
    Investigate differences

    If either result differs, inspect the extracted values, their conversion to integers, the sum, the count, and the final division.

Result

The expected integer averages are 38549 for mbox.txt and 39756 for mbox-short.txt.

integer collectionconverted extracted valuessumall values togetheraveragesum divided by countinteger averagefinal formatted resultcountnumber of valuessum valuescount valuesdividedivisorconvert to integer
How does a collection of integer values become one final integer result?

Tracing an Incorrect Result

Debugging works best when the pipeline is checked in stages rather than only examining the final average. Print or otherwise inspect the intermediate values after extraction, after integer conversion, during the sum and count checks, and after the division and final integer conversion. Each checkpoint helps identify where the result first becomes incorrect.

text matchingcorrect lines identifiedextracted valuesfindall() resultsinteger conversionvalues ready for arithmeticaveragingsum divided by countfinal formattinginteger resultinspect matchesinspect types and valuesinspect sum and countinspect division result
At which step does an incorrect result appear: matching, extraction, conversion, averaging, or final formatting?
  • Common mistake

    Assuming findall() returns integers

    What it looks like

    The extracted value 39772 is treated as though it were already numeric.

    Why it fails

    findall() returns strings, even when the strings contain only digits.

    Do this instead

    Convert each extracted string with int() before arithmetic.

  • Common mistake

    Checking only the final average

    What it looks like

    A result differs from the expected value, but the extracted collection is never inspected.

    Why it fails

    The error could have started during matching, extraction, conversion, summing, counting, or division.

    Do this instead

    Print intermediate values at each stage and locate the first incorrect result.

  • Common mistake

    Capturing the complete formatted line

    What it looks like

    The result includes New Revision: instead of only the numerical part.

    Why it fails

    The regular expression did not isolate the digits with a capturing group.

    Do this instead

    Match the fixed prefix and capture the number that follows it.

Best practice

Treat every stage as a checkpoint: confirm that the intended lines match, confirm that findall() produced the expected number strings, confirm that the values were converted to integers, confirm the sum and count, and finally confirm the integer average.

Practice the Pipeline

MEDIUM

Suppose a collection of consistently formatted revision lines has been processed with a regular expression and findall(). Describe the checks you would perform, in order, before trusting the final integer average.

Hints
  • First check whether the regular expression matched the intended formatted lines.
  • Next inspect the values returned by findall() and remember that they are strings.
  • Then verify integer conversion, the sum, the count, the division, and the final integer conversion.
  1. 1
    A reliable string-to-number pipeline begins with a consistent text format. Use a regular expression that matches the fixed label and captures the following digits, then use findall() to collect every match. Convert the resulting strings to integers before performing arithmetic. Calculate the average from the sum and count, convert the result to an integer, and inspect intermediate values whenever the final result is unexpected.

Key Takeaways

  • A capturing group lets the regular expression isolate the numerical part of a consistently formatted line.
  • findall() returns extracted values as strings, so convert them with int() before arithmetic.
  • An average is calculated by summing the integer values and dividing by their count, then converting the result to an integer.
  • Intermediate checks reveal whether an error began during matching, extraction, conversion, averaging, or final formatting.

Related concepts

More concepts starting with S