PYTHON
  • PYTHON v3
  • Python IDE Setup
  • Python Programming
    • Python for ABS
      • Resources
      • Ch1 - Getting Started
      • Ch2 - Types, Variables and Simple I/O
  • Python For Network Engineers-Kirk Beyers
    • Resources
    • Python Fundamentals
  • Python Inststitute (PCAP)
    • Resources
    • Course Introduction
    • Python Essentials (Part 1)
      • Module 1
        • Fundamentals
        • What is Python
        • Starting your work with Python
      • Module 2
        • The Print Function
          • Section Summary
        • Literals
          • Section Summary
        • Operations- Data Manipulation Tools
          • Section Summary
        • Variables
          • Leaving Comments in Code
          • The input () Function
  • 100 Days Of Code
    • Resources
    • What You Have To Do !!
    • 100 DAY's
      • DAY 1: Working with Variables
      • Day 2: Data Types & String Manipulation
      • Day 3: Control Flow and Operators
      • Day 4: Randomisation and Lists
      • Day 5: For Loops, Range & Code Blocks
      • Day 6: Python Functions and Karel
      • Day 7: Hangman
      • SUMMARY
  • {Pirple}
    • Resources
Powered by GitBook
On this page
  • The print () function - The escapes and new line characters
  • Multiple Arguments
  • Two conclusions can be drawn from this:
  • The print () Keyword Arguments
  • LAB1
  • LAB2

Was this helpful?

  1. Python Inststitute (PCAP)
  2. Python Essentials (Part 1)
  3. Module 2

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

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

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

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

    *

   * *

  *   *

 *     *

***   ***

  *   *

  *   *

  *****

PreviousModule 2NextSection Summary

Last updated 4 years ago

Was this helpful?

Its an arrow pointing up