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:17 Since this is going to be a file with Python source code, I wrap the instructions in triple quotes to define a string literal using Python syntax.

00:27 Let’s go ahead and save this file as exercise_01_01. That will allow me to quickly run the code that I’m about to write and observe its results in the interactive shell over on the left.

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:12 For instance, it’s quite typical for a faster solution to require more memory. Conversely, one that uses less memory will often sacrifice the execution speed.

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.

01:46 In this case, I think that using a multiline string literal seems the most intuitive and suitable, so I will quickly grab these and declare a variable named text just below the instructions.

02:00 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.

02:19 You can see this when I execute the corresponding code snippet in Python’s interactive shell on the left,

02:27 there’s a newline character at the beginning as well as the end of the string.

02:33 Probably the quickest way to suppress them is by formatting the string literal so that it starts on the same line as the variable it belongs to, and so that it ends on the last starship name.

02:46 However, I prefer to use an explicit line continuation represented by the backslash (\) character. This might be a subjective thing, but it makes the string look a bit better to my eyes.

02:58 Some people prefer to rely on the so-called implicit line continuation, which is yet another way of defining such multiline strings in Python, but I’m not going to cover it in this course.

03:11 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 starships.txt.

03:30 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.

03:46 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.

04:01 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 with statement.

04:25 Inside that with statement, I can print our text variable, but instead of printing to the screen or the standard output stream, I’ll specify the file to write to.

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.

04:50 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.

05:05 It’s just a shorthand notation for your home directory, which you can use instead of typing out the entire path.

05:13 The file we’re looking for is named starships.txt, so let’s open it now.

05:20 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.

05:39 While this will work, you can achieve the same result using even fewer characters. That is by calling the .write() method on your file object directly, passing the text variable as an argument.

05:52 Let’s save it one more time and restart the module. Now let’s open the same file again,

06:01 and there’s no blank line anymore. Apart from that, each word sits on a separate line just as instructed.

06:10 I think that pretty much concludes this exercise. See you in the next one.

Become a Member to join the conversation.