Python For GCSE
Output Statements

We use the print statement to output information to the user.

The following example shows how to print blank lines, how to print literal text, print variables and how to combine text and variables.

name = input("Enter your name: ")
age = int(input("Enter your age: "))
height = float(input("Enter your height in metres: "))

print()
print()

print("Just variables")
print()
print(name)
print(age)
print(height)

print()
print()

print("Combining text and variables")
print()
print("Name:", name)
print("Age:", age)
print("Height:", height)

In the previous example, when we combined variables and text, we used a comma to separate the two things. We can keep using commas and have more than that if we like. By default, Python will separate the list of printed items using a space. Do think about how your output is going to look to the user though. The following works but is a little odd to read.

name = input("Enter your name: ")
age = int(input("Enter your age: "))
height = float(input("Enter your height in metres: "))

print(name, age, height)

The print statements default behaviour is to separate the printed items with spaces and to 'press enter' at the end of the line. You can control these things in your print statements if you want. When you want to be able to put a full stop after a variable, you don't really want a space before it.

name = input("Enter your name: ")
age = int(input("Enter your age: "))

print (name, " is ", age, ".", sep="")

You can also specify a character or some text to use to separate items.

name = input("Enter your name: ")
age = int(input("Enter your age: "))
height = float(input("Enter your height in metres: "))

print(name,age,height,sep=",")

The following example shows how to prevent the line break after a print statement,

name = input("Enter your name: ")
age = int(input("Enter your age: "))

print(name,"is ", end="")
print(age, end="")
print(".")