The print () function – Using Multiple
Arguments
Now we are going to try this function with more than one
argument. Here the arguments are separated by commas.
Look at the below example.
We can gain two conclusions from the above
example.
- This function invoked with more than one argument outputs them all on one line.
- This function puts a space between the outputted arguments on its own initiative.
The print () function – The Keyword
Arguments
The mechanism is called keyword
arguments. The name stems from the fact that 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 order to use it, it is necessary
to know some rules:
- a keyword argument consists of three elements:
a keyword identifying the argument (end here) an equal sign (=) and a value assigned to
that argument
- any keyword arguments have to be put after
the last positional argument (this is very important)
print("My name
is","Python.",end="")
print("Monty Python.")
Output -
My name is Python. Monty Python.
The default behavior reflects the
situation where the end keyword argument is implicitly used
in the following way: end ="\n"
print("My name is","Python.",end="\n")
print("Monty Python.")
Output -
My name is Python.
Monty Python.
We've said previously that the print() function separates its outputted arguments with spaces.
This behavior can be changed too.
The keyword argument that can do this is named sep (like separator).
The print() function now uses a
dash, instead of a space to separate the outputted arguments.
print("My", "name", "is",
"Monty", "Python.", sep="-")
Output -
My-name-is-Monty-Python.
print("My","name","is",sep="_",end="*")
print("Monty","Python.",sep="*",end="*\n")
print("Hi")
Output -
My_name_is*Monty*Python.
Hi
print("Programming","Essentials","in",sep="***",end="...")
print("Python")
Output -
Programming***Essentials***in...Python
No comments:
Post a Comment