1.2. Printing#
In order to get the computer to display information to the screen, we use the
print()
function.
Try running the code below!
print("I love Python")
Take note of the following:
print
must be in lower case. If you try usingPrint
, it won’t work! This is because the computer is fussy! It can only understand very specific instructions.'I love Python'
is just a piece of text. This is the information we want to display.We use ‘ ‘ or “ “ to indicate text. (We actually refer to text as a string; more on this later.)
We tell the computer what to display by placing the contents in circular brackets ().
This is the structure of the print() function:
print(information_you_want_to_display)
.
Question
Which of the following code snippets are valid? Select all that apply.
print["I can code!"]
print('Computers are fun!')
print('Programming is awesome!)
print(Developing software is cool!)
print('Programming rules!')
print('Don't code while sleepy!')
Solution
print["I can code!"]
Invalid. Uses square brackets instead of circular brackets
print('Computers are fun!')
Valid.
print('Programming is awesome!)
Invalid. Missing the quote at the end of the string
print(Developing software is cool!)
Invalid. Missing quotes altogether
print('Programming rules!')
Valid.
print('Don't code while sleepy!')
Invalid. Python is using single quotes to identify the start and end of the string. In this case the '
in don’t is registering as a closing quote so python thinks that the string is Don.
Code challenge: Hello World!
Write a Python program which displays the text Hello World!
The output of your program should look like this:
Hello World!
Solution
print('Hello World!')
Code challenge: Practice!
Write a program which displays the following message.
It should look like this:
To become a great Python programmer you need to
practice, practice, PRACTICE!!!
Hint
Use two print()
statements.
Note
Did you know? You can solve this challenge in one line! See what happens when you try print('Hello\nWorld!')
.
Solution
print('To become a great Python programmer you need to')
print('practice, practice, PRACTICE!!!')