ICS3U Python Screen Output

ICS3U Learning Goals

In this ICS3U Grade 11 Computer Science lesson you will be learning how to:

  • Display messages for the user to the screen using a print statement
  • Use escape characters in print statements

Writing Messages to the Screen using Python

Words or sentences in python are called Strings

The command to display things to the screen is the print command

				
					print()
				
			

The statement you want to print goes inside the print command brackets and is surrounded by either

  • double quotes or
  • single quotes

Normally I stick to using double quotes because in other programming languages such as Java and C/C#/C++ that is the convention and single quotes are used for single characters in those languages.

Interactive Learning Activity - Grade 11 Computer Science

Type the following code into the Python Editor.

				
					print("Hello World")
				
			
  • Run the code see how it displays in the output window

Interactive Learning Activity - Grade 11 Computer Science

Type the following code into the Python Editor.

				
					print("Hello")
print("World")
				
			
  • Run the code see how it displays in the output window

Notice that each print statement will print the message on a new line

If you want for some reason to use use multiple print statements but have them display on the same line you can use the end=”” command.   You can put any character you want in the double quotes and that will appear at the end of the print statement and not move the cursor to the next line.  

Interactive Learning Activity - Grade 11 Computer Science

Type the following code into the Python Editor.

				
					print("Hello",end="")
print("World")
				
			
  • Run the code see how it displays in the output window

You will notice there is no space between the words.  If you wanted one, you could add it in manually inside your double quotes

Interactive Learning Activity - Grade 11 Computer Science

A common error you might make is to forget a double quote (You always need a pair) or to forget a bracket (You always need a pair)

Type the following code into the Python Editor and see what happens when you try to run it.

				
					print("Hello"
				
			

Type the following code into the Python Editor and see what happens when you try to run it.

				
					print("Hello)
				
			

These typing errors are called syntax errrors

Python will tell you what error it encountered, but its not always easy to figure out what you did wrong. Each IDE will also give you a different wording for the error.

Helpful Hint:  When python identifies an error on a line, you might want to look at the line above where the error was identified

Certain characters or print options need a special way to print.  These are called escape characters

  • Use \” to print a double quote
  • Use \’ to print a single quote
  • Use \t to print a tab
  • Use \n to go to the next line
  • Use \\ to print a slash

Interactive Learning Activity - Grade 11 Computer Science

Type the following code into the Python Editor.

				
					print("\tBook Title\n\"Harry Potter\"")
				
			
  • Run the code see how it displays in the output window

The print statement prints whatever is inside its pair of double quotes.  

  • A tab is printed first, which is why you see the words Book Title shifted over
  • Harry Potter is printed on the next line because the print statement encountered a new line character.
  • It is printed inside double quotes using the escape character for a quote.
  • Try to see what happens if you take away the slash in front of the double quote.  -> It will get confused about what is part of the print statement and what isn’t

Video Summary

Watch a video of your teacher summarizing the learning content for this section

ICS3U Coding Questions

Try to code the following questions using an IDE of your choice (Spyder, IDLE, Pycharm, etc).  You will save those files on your computer for future reference. 

Each question has:

  • A video of your teacher live coding and explaining the solution
  • The final code used in the video.

Try your best to solve the problems yourself without looking at the solutions.

ICS3U Practice Question

Name Your File:  “ICS3Uinformation.py” 

Write the code that displays the following

  • Your Name
  • Your School and School Board
  • Your Favorite Music, TV Show, Movie, Color
  • Two Additional Pieces of Information about yourself

Make use of 3 escape characters when you do this and the the “end=” statement

				
					print("\n\n\t\tPaul Rogers")
print("Hamilton Wentworth District School Board\n\tGlendale Secondary School")
print("\nMy Favorite Movie Series are:\n\t\"Harry Potter\" & \"Star Wars\"")
print("\nI only ever wear blue shirts",end = " or ")
print("black shirts\n")
print("I have been teaching computer science since 2004")
				
			

ICS3U Practice Question

Name Your File:  “ICS3Ushape.py” 

Draw a picture of a cool shape using escape characters in your solution.  Something like the picture shown.

ICS3U Teacher Picture

				
					print("I think it looks like a face\n")
print("\t \"- -\"")
print("\t  o o")
print("\t[- o -]")
print("\t  \\_/")
print("\t   -")
				
			

Return to ICS3U1 Main Page