The Print Function
print ("Hello World!")
The word print
is a function name.
What happens when Python encounters an invocation like the one above?
Let's see:
First, Python checks if the name specified is legal (it browses its internal data in order to find an existing function of the name; if this search fails, Python aborts the code);
second, Python checks if the function's requirements for the number of arguments allows you to invoke the function in this way (e.g., if a specific function demands exactly two arguments, any invocation delivering only one argument will be considered erroneous, and will abort the code's execution);
third, Python leaves your code for a moment and jumps into the function you want to invoke; of course, it takes your argument(s) too and passes it/them to the function;
fourth, the function executes its code, causes the desired effect (if any), evaluates the desired result(s) (if any) and finishes its task;
finally, Python returns to your code (to the place just after the invocation) and resumes its execution.
The print () function - The escapes and new line characters
\n = newline print() also can be a new line
print("The itsy bitsy spider\nclimbed up the waterspout.")
print()
print("Down came the rain\nand washed the spider out.")
The itsy bitsy spider
climbed up the waterspout.
Down came the rain
and washed the spider out.
If you want to add a backslash into the string, dont forget its escaping nature - you have to double it
print("The itsy bitsy spider\\ climbed up the waterspout.")
print()
print("Down came the rain\\\nand washed the spider out.")
Multiple Arguments
print ("The itsy bitsy spider","climbed up","the waterspout.")
The itsy bitsy spider climbed up the waterspout.
Here we have 3 arguments (all of them are strings) separated by commas. The strings don't have to have spaces between them (as mentioned the commas separate the ARGUMENTS) However when we run the program the spaces between the strings have returned.
Two conclusions can be drawn from this:
a
print()
function invoked with more than one argument outputs them all one one line.the
print()
function puts a space between the outputted ARGUMENTS on its own initiative
The print ()
Keyword Arguments
print ()
Keyword ArgumentsIf you want the print ()
function to change its behaviour a bit, we can use keyword arguments.
The print ()
function has two keyword arguments that you can use:
end
sep
Keyword arguments have some rules:
A keyword argument consists of 3 elements
a keyword identifying the argument (
end
in this case)an equal sign (
=
)a value assigned to that argument
Any keyword assignments have to be put after the last positional argument (this is very important)
END
print("My name is", "Python.", end=" ")
print("Monty Python.")
My name is Python. Monty Python
In this example we made use of the end
keyword argument and set it to a string containing one space.
The end
keyword argument dictates what should be printed after all the arguments have been printed.
The end=" "
causes a space to be printed rather than the default newline character
SEP
As mentioned print() function separates its outputted arguments with spaces. This behaviours can be changed too.
print("My", "name", "is", "Monty", "Python.", sep="-")
My-name-is-Monty-Python
The sep
argument can also be an empty string too
print("My", "name", "is", "Monty", "Python.", sep="")
MynameisMontyPython
print("My", "name", "is", "Monty", "Python.", sep="*")
My*name*is*Monty*Python
Can be used together
print("My", "name", "is", sep="_", end="*")
print("Monty", "Python.", sep="*", end="*\n")
My_name_is*Monty*Python*
LAB1
Modify the first line of code in the editor, using the sep
and end
keywords, to match the expected output. Use the two print()
functions in the editor.
Expected output
Programming***Essentials***in...Python
print("Programming","Essentials","in",)
print("Python")
print("Programming", "Essentials", "in", sep="***", end="...")
Programming***Essentials***in...Python
LAB2
Make the arrow twice as large (but keep the proportions)
Last updated
Was this helpful?