Filter Saved Starship Names (Solution)
00:00 As you can see, I went ahead and prepared not just the usual exercise instructions, but also the solution to the previous exercise. This will help speed things up, as we’ll build on top of it.
00:12 Now because each name of a starship that we want to check is kept on a separate line in the file, we need to take a step back and modify the code so that it reads the file line by line.
00:23 I’m going to iterate over each line in the file and unconditionally print that line, remembering to remove the trailing newline character just for fun. I’ll do it differently this time.
00:35
Instead of telling the print()
function not to append the newline character, I’ll strip it from the right end of the line by calling the .rstrip()
method, there’s also a corresponding .lstrip()
method, which removes the leading whitespace.
00:50
On the other hand, calling the .strip()
method will remove both the leading and trailing whitespace characters from the string.
00:58 Let’s quickly save the file and run it to test. If there are no errors in the code,
01:06
everything seems all right, but we’re only interested in seeing the names that start with the uppercase letter D
, so that would be Discovery
and Defiant
.
01:17
To filter out the other names, we must introduce an if
statement before printing the current line, which will check whether that line starts with the uppercase letter D
.
01:28
Don’t forget to indent the necessary code block under the if
statement to ensure syntactical correctness.
01:36 This means that Python will only print the line if the condition is satisfied. Let’s check it out. There we go. We can see the expected output, which technically ends this exercise.
01:48 However, I’d like to extend it beyond the basics and think about some corner cases. For example, I can imagine someone writing the starship names using lowercase letters or mixed-case letters, potentially with some extra whitespace around them.
02:04 It would be nice to account for that by cleaning the names before checking the condition. You can pause the video now if you want to try implementing this yourself and then resume the video to compare your solution with mine.
02:17 It’ll give you a chance to revisit the knowledge of string manipulation in Python, which was covered in an earlier video course in the same Python Basics series or the corresponding chapter in the book.
02:30 All right. I hope that this wasn’t too difficult. Let me now show you what I had in mind. We can take the line, strip all whitespace characters from both ends of the line, and capitalize it so that the starship name present on that line always starts with a capital letter.
02:49
Finally, we can assign the result of that operation to a new variable with a descriptive name such as cleaned_line
, and we can use it for the if
condition instead of the original line.
03:05 This updated code ensures that we won’t miss out on any starship names regardless of how they’re written in the file. Notice that we still print the original line, though.
03:15 You can try editing the file by hand to add new starship names or alter existing ones, and then run the code again to observe changes.
03:26 Okay, let’s move on to the next section of this course.
Become a Member to join the conversation.