Using Keyword Arguments With print()
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.
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,
sep—and this defines the separator between arguments that you pass to
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.
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.
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.
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."
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.
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
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: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.
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.
And now, similar to this, you can also change the
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,
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.
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.