Split each line as you read it

Each line in the for loop represents a single string containing two pieces of information:

Johnny

8.

, 65

Juan

9.

, 12

Joseph

8.

, 45

Stacey

7.

81

Aideen

8.

, 05

Zack

7.

21

Aaron

8.

, 31

The for loop shredderâ„¢

Brad

4 . 03

Jim

7.91

fcach h* stains a name and a number, as a string-

Janet

Janet

7.49

7.49

You need to somehow extract the score from the string. In each line, there is a name, followed by a space, followed by the score. You already know how to extract one string from another; you did it for Starbuzz back in Chapter 2. And you could do something similar here using the find () method and index manipulation, searching for the position of a space (' ") character in each line and then extracting the substring that follows it.

Programmers often have to deal with data in strings that contain several pieces of data separated by spaces. It's so common, in fact, that Python provides a special string method to perform the cutting you need: split ().

Python strings have a built-in split() method.

To isolate the score, you need to Cut each line in two.

And you'll find that other programming languages have very similar mechanisms for breaking up strings.

split the string

Was this article helpful?

0 0

Post a comment