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


Talk Python's Mastodon Michael Kennedy's Mastodon