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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Using Keyword Arguments With print()

00:00 In the previous lesson, you learned how to use print() to display your output back to the console, and in this lesson, you will learn how to use keyboard arguments to modify how print() displays this output to the console.

00:13 There’s two keyword arguments you’re going to go over, and you can use both to modify how print() displays output to the console. The first one you’re going to look at is the separator—for short, sepand this defines the separator between arguments that you pass to print().

00:27 Then you also have the end keyword that defines the character that comes at the very end of the line. Now, both of these have default values, which is why you don’t need to specify it necessarily.

00:37 The separator has as a default value just a whitespace (" "), and the end character has as a default value a newline ("\n"), which doesn’t actually look like that in your console, but it just looks like a carriage return, so it jumps into the next line—this is what this character means.

00:54 Now I will go back over to the console and adapt this print() a little bit so that you can see those two keyword arguments in action. So, let me refactor this.

01:04 I will make a new variable called old_age and then print out not just this old_age variable, but I will make a little sentence out of it. I will say, "You'll be", this is my first argument to print(), and I separate it with a comma from the second argument, and then let’s pass a third one, "in 50 years."

01:32 So now you’re still calling the print() function but instead of just passing one argument, which you did before by passing this calculation, you’re passing a string, then a number, and then another string.

01:44 And what the print() function does is it converts all the inputs to strings and sticks them together in the way that is defined with the separator and the the end variable.

01:53 So you’ll see these three arguments are separated by a whitespace by default—this is what the separator does—and they will end with a newline by default. Well, let’s look at this in action to get a better idea of what it means.

02:07 If I run this script,

02:10 I get the input, it calculates the age and then outputs one string that is a concatenation of these three items. It’s stuck together here with a separator whitespace and here another separator whitespace and at the very end, there’s a newline that you don’t see right now but you can notice because the prompt here starts in a newline.

02:32 This is the default. Now, you can change these defaults by passing these keyword arguments. So, for example, I can say instead of the default separator, which is this whitespace character, let’s do something else.

02:45 Let’s put a lot of dashes instead. Now, when I run this code again, you will see that instead of these two whitespaces here that separate the different arguments that you pass to print(), there will be dashes coming up. 35, and you can see the separator got replaced from the default value with whatever you passed in here.

03:09 And now, similar to this, you can also change the end character.

03:15 Now, instead of just the default newline, you could, for example, pass a lot of newlines and then a star, and then some other newlines—for example. You can pass anything in there. And now if you run this again,

03:33 ha, you can see this is the output. The sentence is there, You'll be, then the separator’s inputted here, and then at the end of the string there’s a bunch of newline characters, then there’s this little star in between, and some more newline characters.

03:47 Now, this is a not very sensical output here, but you could of course customize this so that it makes more sense for your situation. Let’s run it again.

04:01 So here, you can see that it added a star and then a newline at the end. So, these are the two most useful keyword arguments that you can pass to the print() function to edit how your output gets displayed back to the console.

04:15 And that wraps up this short introduction to collecting input and displaying output in Python, and in the next lesson, in the conclusion, you will go once again over all the functions that you used and just recap what you learned in this course.

Become a Member to join the conversation.