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.

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?

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.

Visual Explanation — How split() Works

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.

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

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.

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.
Conversion map for each field in the example line
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

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
Step 1 — Open the file and read all linesUse 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
Step 2 — Initialize a variable for the total scoreBefore looping, create a variable to accumulate the sum of all scores.
total = 0
3
Step 3 — Loop through each line and parseFor 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
Step 4 — Compute the averageDivide 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
Step 5 — Print the resultFormat the output for readability using an f-string with one decimal place.
print(f"Average score: {average:.1f}") → prints Average score: 91.3

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.

When to use split() vs. a dedicated CSV library
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 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.

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.

From simple split() to professional data processing
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.

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.

Practice Problems

PROBLEM 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].
PROBLEM 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?
PROBLEM 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.
PROBLEM 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.
PROBLEM 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.

Lesson 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