# 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.")`

![](/files/-MG3gGs9yAqxTH2WfsBp)

### 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

If 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:

1. A keyword argument consists of 3 elements
   1. a ***keyword*** identifying the argument (`end` in this case)
   2. an ***equal sign*** (`=`)
   3. a ***value*** assigned to that argument
2. 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<br>

#### 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*`<br>

## LAB1

&#x20;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**\
\&#xNAN;**`Programming***Essentials***in...Python`**

`print("Programming","Essentials","in",)` \
`print("Python")`

`print("Programming", "Essentials", "in", sep="***", end="...")`\
`Programming***Essentials***in...Python`

## LAB2

![](/files/-MG521kJqrhXq3hog_P0)

![Its an arrow pointing up](/files/-MG52DbcoXPK5V6QWZTH)

```
print("    *")
print("   * *")
print("  *   *")
print(" *     *")
print("***   ***")
print("  *   *")
print("  *   *")
print("  *****")
print()
print()
print("    *\n   * *\n  *   *\n *     *\n***   ***\n  *   *\n  *   *\n  *****")

    *
   * *
  *   *
 *     *
***   ***
  *   *
  *   *
  *****

```

Make the arrow twice as large (but keep the proportions)

```
print("    *\n\n   * *\n\n  *   *\n\n *     *\n\n***   ***\n\n  *   *\n\n  *   *\n\n  *****")

    *

   * *

  *   *

 *     *

***   ***

  *   *

  *   *

  *****

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://python.microcisco.com/python-inststitute-pcap/python-essentials-part-1/module-2/the-print-function.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
