Python 3, an Illustrated Tour Transcripts
Chapter: The standard library
Lecture: Walk-through: Print Function

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this video, we're going to look at, open it up in your editor.
0:04 Let's run it and make sure that it works.
0:06 So in order to run it, I right click in PyCharm and say run print_test
0:10 this just validates that it's being run with pytest, in this case it is,
0:15 it says there are 2 failures, let's look at the test here.
0:18 There are 2 failures because there are 2 tests
0:21 this one here that starts with test and this function here that starts with test.
0:24 The description is this comment right here and we need to do what it says.
0:28 It says print the numbers from 10 down to 0 with a space between them
0:32 and a new line at the end.
0:34 And so the default behavior for the print function
0:36 puts a space between a new line at the end, so let's just try and say print nums
0:40 and run that and see what happens here.
0:43 Okay, we still get 2 failures, let's see if the output from pytest helps us at all.
0:50 We have an assertion, the assertion failed and said
0:53 that this string here is not equal to this string,
0:56 the difference between these two strings is
0:59 that this one has a list embedded in it, and this one does not.
1:03 So when we print out an object here, it just prints the __stir__ version of that
1:08 and if we want to make it so rather than the print out a list
1:13 we want the individual items of the list, we need to use what's called unpacking
1:16 so we can just put a star in front of that and that should unpack them.
1:21 Let's try it again, unpack the individual items from that string
1:25 and it looks like we only get one failure now, so we're good with this first one.
1:28 Let's look at the assignment for the second one,
1:31 print the numbers from 10 down to 0 with a - * - between them
1:36 and no new line at the end.
1:38 So print, we're going to say *nums here again,
1:41 and we're going to say sep is equal to - * - and end is equal to a blank string.
1:51 Let's run it and make sure that it works.
1:54 Okay, it looks like it works, let's look at the test a little bit
1:57 and try and dig into what's actually going on here.
2:00 If you'll notice the import here, I'm importing stringIO
2:04 this is an object that behaves as a file buffer,
2:08 and what I'm doing here is patching or monkey patching sys.stdout,
2:12 I'm creating a stringIO instance, and pointing sys.stdout to it.
2:17 So when I call print down here, rather than printing out to the screen,
2:21 it's printing out the string buffer
2:23 and we're pulling the value from that stream buffer out and checking the value of it.
2:28 So that's how our little test is working.
2:30 Hopefully you understand a little bit about print now
2:33 and again, in Python 3 print is now a function
2:36 and we can use these keyword arguments to change the behavior of the function.