The print() function - the keyword arguments
Python offers another mechanism for the passing of arguments, which can be helpful when you want to convince the print() function to change its behaviour a bit.
We aren't going to explain it in depth right now. We plan to do this when we talk about functions. For now we simply want to show you how it works. Feel free to use it in your own programs.
The mechanism is called keyword arguments. The name stems from the fact the meaning of these arguments is taken not from its location (position) but from the special word (keyword) used to identify them.
The print() function has two keyword arguments that you can use for your purposes. The first of them is named end
In the editor window you can see a very simple example of using a keyword argument.
In order to use it, it is necessary to know some rules:
In our example, we have made use of the end keyword argument, and set it to a string containing one space.
Run the code to see how it works.
The console should now be showing the following text:
My name is Python. Monty Python.
As you can see, the end keyword argument determines the characters the print() function sends to the output once it reaches the end of its positional arguments.
The default behaviour reflects the situation where the end keyword argument is implicitly used in the following way: end="\n"
The print() function - the keyword arguments
And now it's time to try something more difficult.
If you look carefully, you'll se that we've used the end argument, but the string assigned to it is empty (it contains no characters at all).
What will happen now? Run the program in the editor to find out.
As the end argument has been set to nothing, the print() function outputs nothing too, once its positional arguments have been exhausted.
The console should now be showing the following text:
My name is Monty Python.
Note: no newlines have been sent to the output
The string assigned to the end keyword argument an be of any length. Experiment if you want.