Parsing a line read from a file

An order log is read from a text file one line at a time, so each line still ends with a newline character. One line is processed like this:


Python
record = "  qty=12,price=4.50,item=pens\n"
clean = record.strip()
fields = clean.split(",")
item = fields[2].replace("item=", "")


The string assigned to record starts with two spaces. Find the length of clean as an integer, the number of elements in fields as an integer, the value of clean.find("price") as an integer, and the string stored in item, typed without quotation marks.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More String Slicing and Methods Questions: