Python Keyword Parameters

In earlier articles about function, we have used parameters and those parameters are replaced with some arguments when a function call takes place.

In this article, you will learn about python functions that takes default values or called it named parameters.

These named parameters have some values assigned to them. You can always override these default values.

Consider the example below.

def add(n1, n2 = 55):
     return n1 + n2

# Using default value need not require second parameter
result1 = add(41)

# Override the default parameter with new value
result2 = add(25, 75)

# Print output

The first function call does not mention the second parameter which automatically calls the default value of n2 = 55. There will not be a Type error.The function continue to evaluate the expression.

The second function call overrides the default value of 55 with 75 and the function completes the expression.

The output of the program is given below.

== RESTART: C:/PythonExercises/Keyword ==

The keyword parameter also applies to built-in functions.

Keyword Parameters Of Built-in Functions

The built-in functions such as print() has keyword parameters with default value.

The print() function has separator (sep) which has a default value of while space.


The output of the above is as follows.

23 55 77

The output above shows that each number is separated by a white space. You can override the default value and put some other character.

Let us modify our previous print statement and use sep='_'.

print(23, 55, 77, sep='_')

The output will have a different separator than white space.It will be replaced with underscore( _ ).


Not only this, the print() function has another keyword parameter called end. The end has a newline character as default value that allows the print() to start with a newline every time.

You can make sure that the print() prints all items in the same line horizontally if you override the default newline.

print(77,88, end=" ")
print(6,25, end=" ")

The output is given below.

44 23
77 88 6 25 

If you look at the output, the first print statement that does not use end is printed on a separate line. It is because the end uses a newline character by default.

The second and the third print statement does not use newline and is overridden by white space at the end of each print statement.

Therefore, it prints all the values horizontally.

Consider an example where we used both sep and end keyword parameter.

print(44,66, sep='@', end='$')

The output is given below.

[email protected]$

Each of the character is separated by @ and the print ends with $ sign.

Skip to content