Write Starship Names (Solution)
00:00 In this lesson, I’ll show you the solution to the first exercise in this section as well as my thought process behind it. I already have IDLE open with the interactive Python shell on the left and the instructions on the right to keep me reminded of what I need to do.
00:42 First of all, let’s acknowledge the fact that there’s no one correct way of solving any programming problem. Even though Python advocates for having only one way to do it, there are usually multiple different paths that ultimately lead to the same outcome.
00:58 So if your solution is a little bit different than mine, but still works, then that’s perfectly fine. In practice, those alternative paths can involve different trade-offs that need to be taken into account.
01:24 Now, the instructions that you see here don’t specify how you’re supposed to store these starship names in Python. You could use a list of strings, for example, or a single string delimited with new lines or other characters, or maybe even some more sophisticated language construct available in Python like an enumeration class.
Then I’ll assign a string enclosed in triple quotes (
"""), which allow me to include multiple lines of text like so. However, because triple quotes preserve whitespace characters, this string actually includes unwanted blank lines around the starship names.
Okay, now that we have a variable with starship names, it’s time to save them in a text file. To do that, I’ll import the
Path object from the
pathlib module, and at the very bottom, I’ll define a path pointing to a file named
Notice, by the way, that I’m quoting the file name because I want to define a Python string. According to the instructions, the file should be located in my home directory, which I can conveniently find by calling
.home() on the
Path data type.
Let’s assign this path to another variable so that I can refer to it later.
path seems a descriptive enough name. Now, using this path, I’ll open a new file in the writing mode because we want to write to it.
I’ll also specify the character encoding as UTF-8, which is always a good practice, although in this case it doesn’t really matter. Finally, to make sure that Python will eventually close this file, reclaiming the associated resources to the operating system and flushing the internal buffers, I’ll wrap this line in a
04:37 I can now save this module and run it to verify if there are any problems. There’s no output, which is a good sign. That means we didn’t have any errors. Therefore, we can assume the file was written successfully.
However, to be completely sure, let’s try to open this file on Unix-like operating systems, including macOS and Linux. You can refer to your home directory using the tilde (
~), which is this little squiggly line character here.
As you can see, there’s an extra blank line at the end of the file, which is the result of calling the
print() function by default. The
print() function includes a trailing newline character, which you can disable by specifying the optional
end parameter with an empty string as a value.
Become a Member to join the conversation.