← VCE Software Development U3O1

Splitting a line into fields with .split()

A string method that breaks a line of text into a list of field strings at each comma. Use strip() first to remove the newline, and int() or float() to convert fields.

Understanding Splitting a line into fields with .split() in VCE Software Development U3O1

What it does: The delimiter passed to split() must match the separator in the data. A tab-separated file needs '\t', not ','. Fields from split() are strings, so float() (or int() for whole numbers) is needed before doing any maths with them. split(',') takes the delimiter as a string argument and returns a list of field strings: ['Ava', '16', '88.5']. Without strip(), the last field would be '95\n' and include the newline character. Strip first, then split. fields[1] is the string '77'. Use int() to convert it. Adding 0 to a string causes a TypeError. '1.5' has a decimal point, so use float(). Calling int('1.5') causes a ValueError.

Common mistakes: split() returns a list, and lists have no strip() method. Strip the newline from the string first, then split: line.strip().split(","). Fields from split() are strings, so adding the number 5 raises a TypeError. Convert the field first: int(row[1]) + 5.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 98–113) and word search (Modules 72–113).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly splits the string line = "Ava,16,88.5" into a list of fields?
fields = line.split()[','])fields = line.split(',')
Matches
fields = line.split(',') name = fields[0] total = fields[1] + 5
↓
The field fields[1] is still a string, so adding the number 5 causes a type error. Convert it first with int(fields[1]) + 5.
Applying
Meanings
Term
line.split(',')
Meaning
A string method that breaks a line of text into a list of field strings at each comma. Use strip() first to remove the newline, and int() or float() to convert fields.
Practice
line = "Ava,7,88.5\n"
Testing
Gaps
fields = line.(',')
Check Answer
Spots
1line = "Ava,88,74\n"
2row = line.split(",").strip()

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Moray Eel' badge — Lurking Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly splits the string line = "Ava,16,88.5" into a list of fields?
A. fields = line.split(',') ✓
B. fields = line.split()[','])
split(',') takes the delimiter as a string argument and returns a list of field strings: ['Ava', '16', '88.5'].
2. What does "Liam,17,72".split(',') return?
A. ['Liam', '17', '72'] ✓
B. ['Liam', 17, 72]
split() always returns a list of strings. The numbers stay as text until you convert them with int() or float().
3. A line read from a file is "Mia,95\n". What should you do first before splitting it?
A. Call int(line) to convert it to a number
B. Call line.strip() to remove the newline ✓
Without strip(), the last field would be '95\n' and include the newline character. Strip first, then split.
4. A file stores each row as Noah;52;58;64. Which delimiter should you pass to split()?
A. line.split(',')
B. line.split(';') ✓
The delimiter you give to split() must match the one the file uses. A comma will not split a line that is separated by semicolons.
5. After fields = "Zoe,77,84".split(','), which line correctly turns the maths mark into a whole number?
A. mark = int(fields[1]) ✓
B. mark = fields[1] + 0
fields[1] is the string '77'. Use int() to convert it. Adding 0 to a string causes a TypeError.

Match the Pairs

line.strip().split(',') → Removes the newline from the end of the line, then breaks it into a list of field strings at each comma
parts = 'Ava,7,88.5'.split(',') → Creates the list ['Ava', '7', '88.5'] where every field is still a string
Choose the delimiter that the file actually uses, such as a tab, by passing it to split() → fields = line.split('\t')
Convert a field to a number after splitting, because split() gives strings → mark = float(fields[2])
fields = line.split(',') name = fields[0] total = fields[1] + 5 → The field fields[1] is still a string, so adding the number 5 causes a type error. Convert it first with int(fields[1]) + 5.
fields = line.split(';') # CSV uses commas → The wrong delimiter is used, so the line is not split and the list has only one item. Use line.split(',') to match the commas.

Key Term

line.split(',')
A string method that breaks a line of text into a list of field strings at each comma. Use strip() first to remove the newline, and int() or float() to convert fields.

Fill in the Blanks

1. line = "Ava,88,74" fields = line.split(',') print(fields[0])
Hint: Type the string method that breaks a line into a list of fields.
2. line = "Ava,88,74\n" clean = line.strip() fields = clean.split(',')
Hint: Type the method that removes the newline from the ends of the line before splitting.
3. line = "Liam;65;70" DELIM = ';' fields = line.split(DELIM)
Hint: Type the name of the variable that holds the delimiter character to split on.
4. fields = line.strip().split(',') mark = int(fields[1]) print(mark + 5)
Hint: Type the function that converts a whole-number field string into a number.
5. line = "Pencil,Stationery,1.5" fields = line.split(',') price = float(fields[2])
Hint: Type the function that converts a decimal field string into a number.

Spot the Error

Question 1
A line = "Ava,88,74\n"
B row = line.split(",").strip()
C name = row[0]
D print(name)
Line B — split() returns a list, and lists have no strip() method. Strip the newline from the string first, then split: line.strip().split(",").
Question 2
A line = "Ben,70,80\n"
B row = line.strip().split(",")
C mark = row[1] + 5
D print(mark)
Line C — Fields from split() are strings, so adding the number 5 raises a TypeError. Convert the field first: int(row[1]) + 5.
Question 3
A line = "Cy,4.5,3\n"
B row = line.strip().split(",")
C price = float(row[1])
D qty = int(row[3])
E print(price * qty)
Line D — The line splits into only three fields, with indexes 0 to 2. Index 3 is out of range. The quantity is row[2].
Question 4
A sep = ", "
B line = "Di,91,85\n"
C row = line.strip().split(sep)
D print(row[1])
Line A — The delimiter must match the data exactly. The fields are separated by a comma with no space, so the delimiter is ",". With ", " nothing matches and row[1] causes an IndexError.
Question 5
A line == input("Name,age: ")
B row = line.strip().split(",")
C age = int(row[1])
D print(age + 1)
Line A — == compares values and does not assign. The typed text is never stored in line, so line is undefined on the next line. Use a single = to assign.

Student with a class code? Join your class →