Filter Saved Starship Names (Solution)
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.
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.
Become a Member to join the conversation.