Splitting a line into fields with .split()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Splitting a line into fields with .split()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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()[','])
2. What does "Liam,17,72".split(',') return?
A ['Liam', '17', '72']
B ['Liam', 17, 72]
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
4. A file stores each row as Noah;52;58;64. Which delimiter should you pass to split()?
A line.split(',')
B line.split(';')
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

line.split(',')

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
line = "Ava,88,74" fields = line._____(',') print(fields[0])
2.
line = "Ava,88,74\n" clean = line._____() fields = clean.split(',')
3.
line = "Liam;65;70" DELIM = ';' fields = line.split(_____)
4.
fields = line.strip().split(',') mark = ___(fields[1]) print(mark + 5)
5.
line = "Pencil,Stationery,1.5" fields = line.split(',') price = _____(fields[2])

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A line = "Ava,88,74\n"
B row = line.split(",").strip()
C name = row[0]
D print(name)
Question 2
A line = "Ben,70,80\n"
B row = line.strip().split(",")
C mark = row[1] + 5
D print(mark)
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)
Question 4
A sep = ", "
B line = "Di,91,85\n"
C row = line.strip().split(sep)
D print(row[1])
Question 5
A line == input("Name,age: ")
B row = line.strip().split(",")
C age = int(row[1])
D print(age + 1)

Answer Key

Splitting a line into fields with .split()
Section A: Quick Questions
Q1
A — fields = line.split(',')
Q2
A — ['Liam', '17', '72']
Q3
B — Call line.strip() to remove the newline
Q4
B — line.split(';')
Q5
A — mark = int(fields[1])
Section B: Match the Pairs
Q1
line.strip().split(',') → Removes the newline from the end of the line, then breaks it into a list of field strings at each comma
Q2
parts = 'Ava,7,88.5'.split(',') → Creates the list ['Ava', '7', '88.5'] where every field is still a string
Q3
Choose the delimiter that the file actually uses, such as a tab, by passing it to split() → fields = line.split('\t')
Q4
Convert a field to a number after splitting, because split() gives strings → mark = float(fields[2])
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
line = "Ava,88,74" fields = line.split(',') print(fields[0])
Q2
line = "Ava,88,74\n" clean = line.strip() fields = clean.split(',')
Q3
line = "Liam;65;70" DELIM = ';' fields = line.split(DELIM)
Q4
fields = line.strip().split(',') mark = int(fields[1]) print(mark + 5)
Q5
line = "Pencil,Stationery,1.5" fields = line.split(',') price = float(fields[2])
Section E: Spot the Error
Q1
B — split() returns a list, and lists have no strip() method. Strip the newline from the string first, then split: line.strip().split(",").
Q2
C — Fields from split() are strings, so adding the number 5 raises a TypeError. Convert the field first: int(row[1]) + 5.
Q3
D — The line splits into only three fields, with indexes 0 to 2. Index 3 is out of range. The quantity is row[2].
Q4
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.
Q5
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.