Since we have already taken a look at positional arguments, let's take a closer look at keyword arguments.
A keyword argument is where you provide a name to the variable as you pass it into the function. Remember that its the name that matters, not the position.
a = 10
In this example, we have assigned a value of "10" to the letter "a" by using the equal sign (=). What happens when we run this code in the Python compiler?
NOTE: Notice that we do not use double-quotes with keywords that have an assigned value.
The end keyword
The end keyword argument determines the characters the print() function sends to the output once it reaches the end of its positional arguments.
print("Twinkle, Twinkle", end=" ")
Python's print() function comes with a parameter called 'end'. You can end a print statement with any character/string using this parameter. The value of this parameter is the newline character, \n.
It is important to note that there is a space between the double-quotes of the end parameter.
*Notice that the end keyword ends the string with a newline.
The sep keyword
The print() function separates its outputted arguments with spaces. One way we can change this behavior is by using the sep keyword. Much like the end keyword, this keyword will be written at the end of the string.
print("Twinkle,","Twinkle,","Little","Star", sep=" ")
Python's print() function comes with a parameter called 'sep'. By using this parameter, we can specify how values are separated in the output.
PracticeAdvice: Add these exercises to your troubleshooting guide for easy access and review!
- becoming familiar with the print() function
- experimenting with Python code
Use the print() function, the new line and escape character to write one line of code (input) which will result in two lines of output.
Use the end parameter to write two lines of code with the result of one line of output.
The keyword arguments end and sep are commonly used together. Input the following into your Python compiler:
print("Twinkle,","Twinkle,","Little","Star", sep="_", end="*")
print("How I wonder what you are")