Learning outcomes
By the end of this lesson you should be able to: index and slice a string exactly as you index a list; split a line of text into words; open a file and read it line by line; and combine file reading with a dictionary to summarise the text inside — which is the capstone pattern you will complete in the final lesson.
Prerequisites
Lesson 8 (Lists & dictionaries). A string is a lot like a list of characters, and file output is collected with the dictionary counting idiom — so you need both ideas already in hand.
The big idea
This lesson is where the course turns a corner. Everything so far worked on values you typed or computed. Files let your program work on the kind of data it exists to process: text someone stored, records from a database, paragraphs from a web page. And the strings you learned about in passing become the real work of the course — text that needs slicing, splitting, and counting. Combine the two — read a file, split each line, count words with a dictionary — and you have the core of the PY4E course in front of you.
Why this matters before the syntax
Strings are the common currency of this course, and they behave like the lists you already know. You index them from zero, you can take a slice s[1:4] to get a substring, and you can use a negative index to count from the end. The reason to care about slicing and splitting is that text almost never arrives in the shape you want. A line of a file like "the cat sat" is one contiguous string, but you usually want its component words — which is exactly what split() gives you.
Files introduce the idea of a stream. When you open a file and loop over it, you get it line by line — the loop's variable is the whole line, including the newline character at the end. This is why stripping matters: a line read from a file often ends with a newline, and if you split without stripping, the last word on the line carries a trailing newline you did not intend. The strip() method removes surrounding whitespace and is the standard first move on every line you read.
The final piece is the pattern that ties it all together: open the file, loop over its lines, split each line into words, and feed each word into the counting dictionary. This is a small program but it is the template for the whole field — reading text, breaking it into tokens, and summarising them. Everything after this lesson is variations on that idea.
Worked example 1: indexing and slicing a string
A string is a sequence of characters, indexed from zero and sliceable the way a list is.
s = "Python"
print(s[0])
print(s[-1])
print(s[1:4])P
n
ythIndexing a string is identical to indexing a list: you count from zero, and a negative index counts from the end, so s[-1] is the last character and s[-2] the second-to-last. A slice s[1:4] is like a substring — it takes the characters at positions 1, 2 and 3, stopping before 4. Once you have seen list indexing, string indexing is not a new idea, just a new thing to apply it to.
Worked example 2: splitting a line into words, and stripping
split() breaks a string on whitespace and returns a list of words. strip() removes surrounding whitespace — including a trailing newline.
line = "the cat sat"
words = line.split()
print(len(words))
print(words[0])
print(repr(" hi \n".strip()))3
the
'hi'The split() method returns a list of the individual words, so the line "the cat sat" becomes a list of three strings and len(words) is 3. The strip() method peels off leading and trailing whitespace — spaces, tabs, newlines — which is why the third line, given " hi \n", yields exactly "hi". In real use, a line read from a file often arrives with a newline on the end, so stripping first is routine.
Worked example 3: reading a file line by line
To read a file, first create it. This example assumes a small file called words.txt with three lines. The open() call hands back a stream and the loop visits it one line at a time.
| words.txt (create this file) |
|---|
| the cat sat |
| the dog ran |
| the cat |
f = open("words.txt", "r")
for line in f:
print(line.strip())
f.close()the cat sat
the dog ran
the catopen("words.txt", "r") opens the file for reading; the second argument "r" is optional for reading but is good to be explicit about. The for loop reads the file one line at a time, with line bound to each full line including its newline. print(line.strip()) is why the output has no blank lines — it trims the newline before printing. Closing the file is polite and, in larger programs, essential so the file is not held open longer than needed.
Worked example 4: the capstone pattern — count words from a file
Now the payoff. Read the same file, split every line into words, and count each word with a dictionary. This is the small program PY4E opens its whole course with.
counts = {}
for line in open("words.txt", "r"):
for w in line.split():
counts[w] = counts.get(w, 0) + 1
print(counts){'the': 3, 'cat': 2, 'sat': 1, 'dog': 1, 'ran': 1}There are three pieces nested together. The outer loop reads each line of the file. For each line, the inner loop splits it into words and visits each one. Each word goes through the counting idiom from lesson 8 — get its current count or 0, add one, store it back. The result is a dictionary whose keys are all the distinct words and whose values are how many times each appeared. Read it slowly and you will see it is just the file-reading pattern wrapped around the counting pattern — and that this small program is the seed of the word-count project you will write in the final lesson.
Common mistakes
Watch outNot stripping a line before splitting. A line read from a file keeps its newline, so the last word of each line has one attached. strip() trims it.
Watch outForgetting to close the file. Forgetting f.close() may not break a small program, but it holds the file open and can cause problems. Prefer closing, or use a with block.
Watch outConfusing split with strip. split() breaks a string into a list of pieces; strip() only trims the surrounding whitespace. They do different jobs — usually used together.
Watch outExpecting a single split() argument to count words. split() with no argument splits on any whitespace and skips empties, which is usually right. Split on a single space to preserve empties, which is rarely what you want.
Check your understanding
What is s[1:4] if s is "Python", and why does it include those particular characters?
Why does a line read from a file often need strip()?
In the word-count capstone, what does the inner for w in line.split() do?
Multiple choice
What does "Python"[1:4] evaluate to?
Which method turns "the cat sat" into a list of words?
Why should you strip each line read from a file?
Which statement correctly opens a file for reading?
In the inline for line in open("words.txt", "r"):, what does the loop variable line hold?
What is the main purpose of the strip() method?
After the capstone program runs, what does its result dictionary contain?
Which two pieces does the capstone pattern combine?
Summary and next
A string is a sequence you index and slice like a list. split() breaks it into words, strip() trims surrounding whitespace, and open() lets you read a file line by line. The capstone — read a file, split each line, count words in a dictionary — links the last two lessons and is the heart of the whole course.
Next: decomposition. You have now used functions and data structures. The next lesson pulls them together into the higher-level skill of breaking a real problem into functions — the difference between writing a program and designing one.