Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Leveraging print() for String Concatenation

00:00 When you have a collection of strings and you’d like to send them to some output like the terminal, or write them to a file, you might think you need to concatenate them first, but you can actually avoid that step and have the print() function take care of it instead. The print() function performs concatenation when passed multiple string objects.

00:18 It does this by taking a sep argument that becomes the separator for concatenation, much like you saw with the .join() method earlier.

00:25 But what’s really cool is that the print() function can write directly to a file using the file argument. So bring up your Python REPL one more time and we’ll dig into these features.

00:36 Start by defining a list of strings called words. words = [ and the following strings: "Hello, ", "World!".

00:47 "I "am" "a" "Pythonista!" ]. Now let’s have the print() function concatenate these words into one line. Because each argument should be passed separately, we apply the star operator to the list, which in this case, becomes the unpacking operator because it unpacks the elements of the list and the print() function treats them all as separate arguments.

01:10 print(*words) The result: Hello, World! I am a Pythonista! and you’ll note spaces have been added between each word. That’s because a single space is the default argument for the sep parameter of the print() function.

01:25 Now let’s try passing a newline character as the separator instead: print(*words, sep="n")

01:35 Because the separator is now a newline, we see each word on a new line. And what about writing to a file? To do that, we’ll need to open a new file and pass it as an argument to the print() function’s file parameter. with open( "output.txt", "w")

01:55 as output:

01:57 and in an indented block, print(*words,

02:02 sep="n", file=output). The first line of code creates a context manager. It opens a file in write mode called output.txt, or creates it if it doesn’t exist, and assigns the Python file object to the variable output.

02:20 Then we direct the print() function to write to the file by passing output as the argument to the file parameter.

02:26 When the code in the indented block finishes, the context manager closes the file, releasing the related resources and keeping your operating system happy.

02:35 If you want to confirm the file exists, close the REPL by calling exit() and run the following command: cat output.txt. The cat program is used to display the contents of a file, and what you’ll see in the terminal is your newly created file.

02:52 Now it’s time to bring it all together and review what you’ve learned.

Become a Member to join the conversation.