Concepts / String-to-Number Conversion in Python

String-to-Number Conversion in Python

Extract numbers from formatted text lines using a regex pattern with a capturing group and the findall() method.

  • 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.

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

matchmatchfindall() collects captured valuesNew Revision: 39772formatted text lineprefix plus captureddigitsregular expression39772, 39726strings returned byfindall()New Revision: 39726formatted text line
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.

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.

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().

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

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

Collect every match: findall() gathers the captured numerical parts from all matching lines.

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
convertinteger result42string from findall()int()explicit conversion42integer for arithmetic
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.

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

Checking the Expected Average

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

Validate the full-file result: For mbox.txt, compare the program's final integer average with the expected result of 38549.

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

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

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

sum valuescount valuesdividedivisorconvert to integerinteger collectionconverted extracted valuessumall values togetheraveragesum divided by countinteger averagefinal formatted resultcountnumber of values
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.

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

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

    Fix: Convert each extracted string with int() before arithmetic.

  • Checking only the final average

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

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

  • Capturing the complete formatted line

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

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

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. 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.