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

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MG0ftIWXXM4lEGrTAmE%2F-MG3gGs9yAqxTH2WfsBp%2Fimage.png?alt=media\&token=eb7b38f2-6e5b-4ead-9481-5f892df6b93f)

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


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

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

![](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MG4-d7oMnx_JnTkbaNu%2F-MG521kJqrhXq3hog_P0%2Fimage.png?alt=media\&token=46d06312-b173-4a28-8823-789c2a5bb832)

![Its an arrow pointing up](https://3018064759-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LudO0LP3BbNbyBiBB3l%2F-MG4-d7oMnx_JnTkbaNu%2F-MG52DbcoXPK5V6QWZTH%2Fimage.png?alt=media\&token=b0909e2c-c81e-489d-b38a-6f8cc33044cd)

```
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  *****")

    *

   * *

  *   *

 *     *

***   ***

  *   *

  *   *

  *****

```

