Computer Programming 1 (Python) • Strings & Text Processing

Parsing Structured Text — I can parse simple structured text (CSV-like lines without quotes) using split and type conversion at my level.

Learn to break apart comma-separated data and convert each piece into a usable Python value.

A Python coding terminal with a program flow and laptop.
Inside this lesson 10 sections 5 questions 6 figures

SECTION 1

Historical Context & Motivation

Long before spreadsheets and databases became everyday tools, people needed a way to store tables of information in plain text files. The solution was remarkably simple: put each record on its own line and separate the fields with a special character, most often a comma. This idea became known as CSV — short for Comma-Separated Values. Understanding how to read and break apart these lines is one of the most practical skills you can learn in programming, because real-world data often arrives in exactly this format.

1960s • Early Mainframe Data

IBM mainframes stored data in fixed-width and delimited text files. Programmers wrote custom parsers in FORTRAN and COBOL to read each field from a line of text.

1972 • The C Language & Strings

The C programming language introduced powerful string-manipulation functions like strtok(), making it easier to split lines on a delimiter character.

1991 • Python Is Born

Guido van Rossum released Python with a built-in split() method on strings, dramatically simplifying text parsing for beginners and professionals alike.

2005 • CSV Standardized (RFC 4180)

The Internet Engineering Task Force published RFC 4180, formally describing the CSV format. This standard helped tools and languages handle edge cases like quoted fields and embedded commas.

Today • Data Everywhere

CSV remains one of the most common formats for exporting data from spreadsheets, APIs, and scientific instruments. Every programmer eventually needs to parse it.

The core question this lesson addresses is straightforward: when you receive a line of text like "Alice,92,3.8", how do you turn it into individual Python variables — a string for the name, an integer for the score, and a float for the GPA — so your program can actually use them?

SECTION 2

Core Principles & Definitions

Parsing structured text involves two main operations. First, you split the line into individual pieces using a delimiter. Second, you perform type conversion (also called type casting) to change each piece from a raw string into the data type your program actually needs — an int, a float, or even a bool. These two steps form the foundation of all text-based data processing.

1

Delimiter

A character that marks the boundary between fields. In CSV data the delimiter is a comma ",", but tabs, pipes "|", and semicolons are also common.
2

split() Method

A built-in string method in Python that returns a list of substrings. You call it as line.split(",") to break on commas.
3

Type Conversion

Using functions like int() and float() to transform a string of digits into a numeric value your program can do math with.
4

strip() Method

Removes leading and trailing whitespace (spaces, newlines). Often used right after splitting because raw data can include extra spaces around commas.
5

Unpacking

Assigning the elements of a list to individual variables in a single line, like name, score, gpa = parts. This makes your code cleaner and more readable.
✦

KEY TAKEAWAY

Think of a CSV line like a sentence where commas separate distinct ideas. The split() method is like a pair of scissors that cuts the sentence at every comma, giving you a neat stack of individual cards. Type conversion is then like sorting those cards into the right drawer — numbers go to the number drawer, text goes to the text drawer — so you can find and use each piece properly.

SECTION 3

Visual Explanation — How split() Works

How split(",") Breaks a CSV Line into a List"Alice,92,3.8"← original string.split(",")"Alice"index 0 — str"92"index 1 — str"3.8"index 2 — strstr (keep)int()float()"Alice"name: str92score: int3.8gpa: floatPython code:parts = "Alice,92,3.8".split(",") → name, score, gpa = parts[0], int(parts[1]), float(parts[2])
The top box shows the original string. After split(","), three string fragments appear in a list. Type-conversion functions then transform each fragment into the appropriate Python data type shown at the bottom.

Notice that after the split, every element in the resulting list is still a string. The value "92" looks like a number, but Python treats it as text until you explicitly convert it with int(). Similarly, "3.8" must pass through float() before you can use it in arithmetic. Forgetting this conversion step is one of the most common bugs beginners encounter, because Python will happily let you "add" two strings — it just concatenates them instead of performing math.

SECTION 4

How It Works — The Parsing Pipeline

Parsing a CSV-like line follows a predictable pipeline. Every time you encounter structured text, you apply the same sequence of operations. Let's formalize each stage so you can apply it confidently to any dataset.

Stage 1 — Read the Raw Line

The data arrives as a single string. It might come from a file via file.readline(), from user input via input(), or from any other text source. At this stage, the entire line — including the trailing newline character "\n" — is one long string.

Stage 2 — Strip Whitespace

Calling line.strip() removes any leading or trailing whitespace, including the newline at the end. This prevents invisible characters from sneaking into your data and causing conversion errors later.

Stage 3 — Split on the Delimiter

Call line.split(",") to produce a list of string fragments. The number of fragments equals the number of delimiters plus one. For example, a line with two commas yields three fragments.

FRAGMENT COUNT

fragmentcount=delimitercount+1fragment_count = delimiter_count + 1

A line with n commas produces n + 1 string elements in the resulting list.

Stage 4 — Convert Types

For each fragment, decide whether it should remain a string or be converted. Use int() for whole numbers, float() for decimal numbers, and leave text fields as strings. If a fragment cannot be converted — say you call int("hello") — Python raises a ValueError.

Stage 5 — Assign to Variables

Finally, store each converted value in a descriptive variable. You can either index into the list (parts[0], parts[1], etc.) or use tuple unpacking to assign all values in one clean line.

⚠

Common Pitfall

If you try int("3.8"), Python will throw a ValueError because int() cannot handle a decimal point. You must use float("3.8") first, and then convert to int with int(float("3.8")) if you need a whole number.

SECTION 5

Data Flow — From Raw Text to Usable Values

Let's trace a more realistic example. Imagine you have a file of student records where each line looks like "Bob,85,3.4,True". You want the name as a string, the exam score as an integer, the GPA as a float, and the honors status as a boolean. The diagram below shows every transformation step.

Parsing Pipeline: Multi-Field CSV Line"Bob,85,3.4,True\n"raw line.strip()"Bob,85,3.4,True"cleaned.split(",")"Bob"[0]"85"[1]"3.4"[2]"True"[3]keep as strint()float()== "True""Bob"name: str85score: int3.4gpa: floatTruehonors: boolComplete Code:line = "Bob,85,3.4,True\n"parts = line.strip().split(",")name, score, gpa, honors = parts[0], int(parts[1]), float(parts[2]), parts[3]=="True"
This diagram traces a four-field CSV line through the full pipeline: strip whitespace, split on commas, then convert each field to its target type. The boolean conversion uses a comparison parts[3] == "True" because Python's bool() treats any non-empty string as True.
Field PositionRaw Value (str)ConversionResult
parts[0]"Bob"None (already str)"Bob"
parts[1]"85"int()85
parts[2]"3.4"float()3.4
parts[3]"True"== "True"True

Conversion map for each field in the example line

SECTION 6

Worked Example — Processing a Mini Gradebook

Suppose you have a file called grades.txt with these three lines:

📝

grades.txt

Alice,92,3.8 Bob,85,3.4 Carla,97,4.0

Your goal is to read these lines, parse each one, and compute the average exam score across all students.

Parse grades.txt and compute average score

  1. 1

    Step 1 — Open the file and read all lines

    Use Python's open() function to read the file. The readlines() method returns a list of strings, one per line. Each string includes a trailing newline character.
    lines = open("grades.txt").readlines() → ["Alice,92,3.8\n", "Bob,85,3.4\n", "Carla,97,4.0\n"]
  2. 2

    Step 2 — Initialize a variable for the total score

    Before looping, create a variable to accumulate the sum of all scores.
    total = 0
  3. 3

    Step 3 — Loop through each line and parse

    For each line, call strip() to remove the newline, then split(",") to break it into parts. Extract the score at index 1 and convert it to an integer.
    for line in lines: parts = line.strip().split(",") score = int(parts[1]) total += score
  4. 4

    Step 4 — Compute the average

    Divide the total by the number of lines. Use len(lines) to get the count.
    average = total / len(lines) → total is 92 + 85 + 97 = 274, so average = 274 / 3 ≈ 91.33
  5. 5

    Step 5 — Print the result

    Format the output for readability using an f-string with one decimal place.
    print(f"Average score: {average:.1f}") → prints Average score: 91.3

SECTION 7

Strengths & Limitations of Simple split() Parsing

Using split() to parse structured text is fast, simple, and requires no extra libraries. However, it has important limitations you should understand so you know when it works well and when you need a more powerful tool.

Strengths ✅Limitations ⚠️
Built into Python — no imports neededCannot handle commas inside quoted fields (e.g., "Smith, Jr.")
Easy to read and understand for beginnersNo automatic type detection — you must know each field's type in advance
Works perfectly for clean, predictable dataCrashes on malformed lines (wrong number of fields, non-numeric data)
Fast — no overhead from parsing librariesDoes not handle multi-line fields or escaped characters
Flexible — works with any delimiter, not just commasFor complex CSV, Python's csv module is safer and more robust

When to use split() vs. a dedicated CSV library

🔑

WHEN TO LEVEL UP

Think of split() like a basic kitchen knife — it handles most everyday cutting tasks perfectly. But if your data has commas embedded inside fields (like addresses), you need the specialized tool: Python's csv module, which acts like a mandoline slicer that handles tricky edges safely.

SECTION 8

Connection to Advanced Text Processing

The simple parsing skills you learn here form the foundation for more advanced data processing techniques you will encounter as you progress in computer science. Understanding how to manually split and convert gives you insight into what higher-level tools do under the hood.

What You Learn NowWhat Comes Next
line.split(",")Python's csv.reader() handles quoted fields, escaped characters, and different dialects automatically.
Manual int() / float() conversionLibraries like pandas infer data types automatically when reading files with pd.read_csv().
Parsing one line at a time in a loopDataFrames let you operate on entire columns at once, processing millions of rows efficiently.
Simple ValueError crashes on bad dataTry/except blocks and data validation libraries provide graceful error handling.
CSV (flat text)JSON, XML, and databases offer hierarchical and relational data structures.

From simple split() to professional data processing

Even professional data scientists occasionally use split() for quick-and-dirty parsing of log files or configuration data. Mastering this fundamental technique gives you a reliable tool you can always fall back on, and it builds the intuition you need to understand why higher-level libraries work the way they do.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

After running parts = "10,20,30".split(","), what is the data type of parts[1]? Explain why, and describe what would happen if you tried parts[0] + parts[1].
Show answer
The data type of parts[1] is str (string), because split() always returns a list of strings, regardless of what the text looks like. If you run parts[0] + parts[1], Python will concatenate the strings, giving you "1020" instead of the number 30. To get numerical addition, you would need to convert first: int(parts[0]) + int(parts[1]) which equals 30.

Question 2: BASIC CALCULATION

Given the string line = "Laptop,999.99,3", write Python code to parse this line and compute the total cost (price × quantity). What is the final numeric result?
Show answer
First, split the line: parts = line.split(",") gives ["Laptop", "999.99", "3"]. Then convert the price to float and quantity to int: price = float(parts[1]) and qty = int(parts[2]). The total cost is total = price * qty, which equals 999.99 × 3 = 2999.97. Note that you must use float() rather than int() for the price because it contains a decimal point.

Question 3: INTERMEDIATE

You have a multi-line string stored in a variable: data = """Math,88\nScience,95\nEnglish,72\nHistory,90""" Write a program that parses this data and prints the name of the subject with the highest score.
Show answer
First, split the data into individual lines using data.strip().split("\n"). Then loop through each line, split on commas, and track the highest score. A clean solution looks like this: best_name = "" best_score = -1 for line in data.strip().split("\n"): parts = line.split(",") name = parts[0] score = int(parts[1]) if score > best_score: best_score = score best_name = name print(best_name) This prints Science because 95 is the highest score. The key insight is combining line-splitting (split("\n")) with field-splitting (split(",")) and type conversion to find a maximum.

Question 4: APPLIED

A weather station logs data as pipe-separated values. Each line has the format "date|high_temp|low_temp|rainfall_mm". Given this sample: "2025-01-10|42.5|28.3|0.0\n2025-01-11|38.1|25.7|12.4\n2025-01-12|45.0|30.2|0.0", write code to parse all lines and compute the average daily temperature range (high − low) across all three days.
Show answer
Since the delimiter is a pipe character, use split("|") instead of split(","). The code: log = "2025-01-10|42.5|28.3|0.0\n2025-01-11|38.1|25.7|12.4\n2025-01-12|45.0|30.2|0.0" total_range = 0 lines = log.strip().split("\n") for line in lines: parts = line.split("|") high = float(parts[1]) low = float(parts[2]) total_range += high - low avg_range = total_range / len(lines) print(f"{avg_range:.1f}") The daily ranges are 42.5 − 28.3 = 14.2, then 38.1 − 25.7 = 12.4, then 45.0 − 30.2 = 14.8. Total = 41.4, divided by 3 = 13.8. This problem demonstrates that split() works with any single-character delimiter.

Question 5: CRITICAL THINKING

Consider this data line: "Widget,12,N/A,4.5". The third field is supposed to be an integer, but someone entered "N/A" instead. What error will Python raise if you call int(parts[2])? Propose a strategy using an if-statement (no try/except required) to handle this gracefully by substituting a default value of 0 when the field is not a valid number.
Show answer
Python will raise a ValueError because the string "N/A" cannot be converted to an integer. A simple solution uses the string method isdigit(): value = int(parts[2]) if parts[2].isdigit() else 0. The isdigit() method returns True only if every character in the string is a digit. For floats, you could check with parts[2].replace('.','',1).isdigit(). This approach is a form of data validation — checking input before processing it — which is a crucial real-world programming practice that prevents your program from crashing on unexpected data.

SUMMARY

Lesson Summary

Summary

Parsing structured text is a foundational programming skill. You start with a raw line of CSV-like text and apply the strip() method to remove unwanted whitespace. Next, you call split() with a delimiter (such as a comma) to break the line into a list of string fragments. Each fragment is then passed through a type conversion function — int(), float(), or a boolean comparison — to transform it into the data type your program needs.

This technique works perfectly for clean, predictable data without embedded delimiters or quoted fields. When you encounter messier data, Python's csv module and libraries like pandas provide more robust parsing. But the mental model remains the same: split, convert, and assign. Mastering this simple pipeline prepares you for every text-processing challenge ahead.

Varsity Tutors • Computer Programming 1 (Python) • Parsing Structured Text