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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
line.split(',')Fill in the Blanks
Spot the Error
Student with a class code? Join your class →