Python 3, an illustrated tour Transcripts
Chapter: The standard library
Lecture: Print Function
0:00 In this video we're going to talk about the print function.
0:02 One of the biggest changes for Python 3 is the print function.
0:07 In Python 2 print was not a function, but it was a statement, it was built in into the language
0:13 and that's changed in Python 3 just to make things a little bit more consistent.
0:16 Guido wanted to change that into a function.
0:20 Let's look at some of the features or changes that that brings.
0:23 In Python 3, rather than calling print as a statement, we call it as a function
0:29 and so you'll note that there are now parentheses
0:32 when we call something as a callable in Python, we insert parentheses there.
0:36 So we're invoking print and we're passing in one and the string one as parameters
0:43 and note that Python prints out 1 and puts a space in there and then print another one.
0:48 It also puts a new line at the end here that you can't really see, but it's doing that.
0:53 Now if we change this a little bit, so one of the things that Python 3 brings about
0:57 is it allows us to use keyword arguments,
1:00 And we can use 2 keyword arguments with the print function
1:03 sep and end, and sep is what goes in between the arguments that we provide
1:07 and end is what goes in the end.
1:10 The default sep is a space and the default for end is a new line.
1:13 You'll note that I change them here and we see that I've changed the output here,
1:17 it doesn't put a new line at the end.
1:19 Here's just another slide showing that the sep comes in between the arguments
1:23 and end comes at the end,
1:25 if we have multiple arguments sep will be inserted between each one of those.
1:28 And that's it, there isn't that much to it
1:30 other than this is meant to be a thing that makes Python more consistent
1:34 and to eliminate some of the statements in Python 2.
1:38 I'll just go on a little rant here about print
1:40 I personally think that you shouldn't check-in print into your code.
1:43 If you need to print something you're probably either logging it
1:47 or wanting to log it for debugging purposes.
1:50 So you should use the logging module for that.
1:52 And if you want to print something out to the screen,
1:55 you can be slightly more explicit by calling the write method
1:58 on the sys.stdout attribute found in the system module,
2:02 that will write out to the standard out which typically writes out to the screen.
2:06 I think that's a little bit more explicit and conveys your intention,
2:10 whereas print, it's not sure whether you want something go out to the screen always
2:14 or whether you just want it there for debug purposes.
2:16 So if you sort of draw a line in the sand and say
2:19 if I need to print something for debug, I'm going to use logging,
2:22 if I need to print something out to the screen in production code or whatnot,
2:25 I'll call sys.stdout.write
2:28 Thanks for watching, I hope you learned a little bit about the print function in this video.