Lesson 1: Printing

Python is a beast of a programming language, but it doesn't have to be scary. In today's lesson, we'll start at the simplest and easiest starting point we can.

Printing

The very first thing everyone learns when they start coding is how to show some text on the screen. That's called printing.

We can have the computer show stuff to us by using a

print
statement. Printing something is as simple as this:

  1. Type out the word print, followed by two parentheses
  2. Put whatever you want to print inside the parentheses, in quotations.

You might be wondering what we can print in Python. Let's start by printing some words. Try running the code below:

Congratulations! This is your first Python program!

But what did we actually do, though? We printed the phrase "hello world!" to the output console. The output console is a simple screen that shows you just text (no windows or fancy graphics).

Strings

Notice how Python only prints whatever we put inside the parentheses and without the quotation marks that we added. Why was that?

"hello world!"
is an example of a string. A string can be thought of as text encased in quotes. We can put almost any character from the keyboard inside quotes and they will be considered as strings. This even includes numbers, symbols, or even absolutely nothing.

Every time we write a new

print()
statement, Python will write whatever we wrote on a separate line. What does the following print in a console?

If you don't put anything in the parentheses of a

print
statement, it will give you an empty line. Compare the code below to the one above and run it: What do you notice?

Using empty print statements throughout your code is helpful when you want to space out your text.

Printing Things Next To Each Other

You can print two strings next to each other in one

print
statement in one of two ways.

The first way is by using concatenation. Concatenation is a fancy term that just means combining two strings together. All you need to do is to put a

+
symbol between two strings.

Concatenation directly combines strings from end to end, so you may need to put a space in one of the strings if you want it to print out like you want. What's the difference in output between these?

An alternative way is to use commas instead. This will put a comma in between the strings for you. What's the difference in output between these?

Rules for Printing Strings

You may have heard of the term "grammar" before. Grammar refers to the rules that someone speaking a language needs to follow so their sentences make sense. For example, "Today I went to the store to buy candy" makes grammatical sense. "Candy went me store" doesn't make any grammatical sense at all, and we really hope you don't talk like that.

In the same sense, programming languages have their own grammar, under a different name, called syntax. Computers are much more particular about following syntax than your English teacher might be about correct grammar. If your code isn't formatted correctly, your computer literally doesn't know what to do. More likely than not, your program will crash (meaning it stops abruptly) and your computer will spit out a

SyntaxError
. A SyntaxError means that you wrote code that doesn't follow the rules.

What does Python's syntax look like? Well, there's a lot of rules, but so far you probably have at least identified a few dos and don'ts.

For example, in Python, a string is identified by any text enclosed in quotations. If you don't enclose your string in a pair of quotation marks, your computer won't run your program You can use either single quotation marks

' '
or double quotation marks
" "
. Normally, using double quotation marks is the safe way to go so that you don't run into any errors. However, sometimes it will be better to choose single over double.

Take for example, this scenario: try printing the sentence

'I don't know how to code yet, but I am learning!'
using single quotation marks inside the print parentheses. Why won't this program run?

When first learning how to code you might encounter some errors when typing out your code. Python likes to be very precise on how it wants code to look before running it. Every detail matters! To make sure that your code is ready to run, follow some of these rules so that you don't get a

SyntaxError
:

  • Make sure that the word
    print
    is all lowercase
  • The parentheses of your print statement should be closed. This means that you need to make sure you have both sides of the parentheses, one at the beginning and one at the end.
    (...)
  • Make sure that your strings have closed quotation marks. Again, make sure you have both sides of the quotation marks.
    "..."