That Blue Square Thing

AQA Computer Science GCSE

This page is up to date for the AQA 8525 syllabus for exams from 2022.

Programming Concepts - Print Statements

Print statements simply allow something to be displayed (printed) on the screen.

These are crucial to almost every program as they provide the main way to communicate information from the program to the user. This includes printing any instructions that the user needs to know about, telling them what data to enter and outputting the results of the program.

In general this is pretty straightforward stuff...

Simple Printing:

Pseudocode uses the command OUTPUT whereas Python uses the command print().

OUTPUT "Hello world"

Printing in Python needs to open and close the brackets correctly - otherwise you'll get a syntax error.

print("Goodbye cruel world")
print() # prints a blank line

Simple printing is really basic. Using blank lines to make output easier to read on the screen is a really good tip.

Printing variables is a little more complex, but works in pretty much the same way.

myString <- "Hello"
myInteger <- 73

OUTPUT myString
OUTPUT myInteger

The same sort of thing in Python:

myString = "Goodbye"
myInteger = 146

print(myString)
print(myInteger)

Really, the trick to to make sure brackets and quote marks are closed properly.

Concatenation:

Concatenation involves combining text and variables in the same print statement. It's an important way to make the output from your programs more readable.

So, instead of going:

OUTPUT "Your favourite country is"
OUTPUT faveCountry

You can go:

OUTPUT "Your favourite country is " + faveCountry

This prints the output over one line rather than two.

The biggest problem with concatenation is when you try and combine different types of data - in particular, trying to combine a string with a number. You can't do this immediately and have to do some conversion:

OUTPUT "Your test score was " + INT_TO_STRING(testScore)

This converts the integer value of testScore to a string so that it can be concatenated with the string "Your test score was ".

This is crucial to do and will cause a run time error in Python if you don't - which will crash the program.

The Python code here would be:

print("Your test score was " + int(testScore))

There is a lot more about concatenating properly on the Unit 3 String Handling page. This includes pseudo and Python code to handle concentating different data types.

Printing a Blank Line

Using blank lines to divide up output is helpful. There are two ways to do this.

I prefer to use a separate line of code to do it as it helps keep my code cleaner and easier to read. I do this using:

print("First line of text")
print() # blank line
print("Second line of text")

Lots of people seem to not like doing it that way though and it's possible you might see the other method used in an exam. So here it is:

print("First line of text\n\nSecond line of text")

In this method the \n creates a new line; you need two because you want a blank line as well. Watch where you put spaces.

Now, I prefer the first method because, in my view, it makes it way clearer what's going on. I think it's easier to read, easier to code and much, much easier for someone else to look at and maintain. But it does take two more lines of code and, for some people, that's a big thing.

Another, probably much cooler, way of doing this is shown below, although that can't be used if you're including variables and so on.

Mutiple Line Printing

Sometimes you want to print more than one line of text. I often find this is the instructions at the beginning of a program or the options in a menu system.

There is a Python shortcut to reduce the number of instructions required in the program.

print("""Comedy Dog Top Trumps

Enter 1 to 4 to choose what you want to do:

1: Play a new game
2: Resume a saved game
3: View high scores
4: Exit the game"""
)

This will display all 8 lines of text in a single block and using a single print() command.

The three quote marks are what makes this possible. It's almost inevitable, by the way, that you will forget the closing bracket at the end of the code...