Python print()

Find yourself a Python environment such as repl.it and type this program into your editor.
(Don’t copy and paste. Practice getting the details right.)

print('Hello World!')

Run your program. You should see the text output in a different area of your screen.

This print() command is a Python function that outputs text to your console.

print() takes one or more inputs, which are called arguments. These go inside the parentheses.

You can include more than one argument, separating the arguments with commas.

print('cat', 'dog', 'mouse')

Python adds spaces between the different arguments when you separate them with commas:
OUTPUT:

cat dog mouse

Each time you call a print() function, Python starts a new line of text:

print('mat')
print('fog')
print('house')

OUTPUT:

mat
fog
house

The text inside the parenthese is inside quotes.

Quoted text makes a string literal, which means you literally want to print the text inside the parentheses.

If you don’t include the quotes, Python thinks you’re trying to print the contents of a variable, which is a memory location where data can be stored.

Try this:

print(stuff)

OUTPUT:

NameError: name 'stuff' is not defined

Python can’t find any variable named stuff, because you haven’t created it.

Let’s fix that by creating a stuff variable and assigning a value to it:

stuff = "How about this weather?"
print(stuff)

Run that program and verify that it works.

Now change the program and add quotes around “stuff.”

stuff = "How about this weather?"
print("stuff")

What happens?

By the way, in Python you can use either single quotes or double quotes.

Let’s forget one of the quotes. What error message do you get?

print("stuff)

OUTPUT:

    print("stuff)
                ^
SyntaxError: EOL while scanning string literal

“EOL” means “End of line.” Python was expecting a second double quote, but it never found one. By adding the first quote, we started a string literal, but we never closed the quote.

You can get the same type of error if you mix up single and double quotes:

print("stuff')

Try this one:

print('I can't believe we get paid for this.')

This produces an extra error, but this time it’s because Python thought your apostrophe in “can’t” was actually the end of a string literal. It got confused by everything that came afterwards.

Two ways to fix that type of issue:
1) Use double quotes on the outside and a single quote on the inside:

print("I can't believe we get paid for this.")

2) Use an escape character, starting with a backslash \

print('I can\'t believe we get paid for this.')

Notice that can’t became can\’t.

Often text editors will highlight your string literals to alert you to problems with this:

PyCharm example:

Repl.it example:

If you notice that half of your text isn’t highlighted in the same color, this should give you an idea that your quotes or other symbols have an issue.

More on Escape Characters

  • \ Backslash is above “Enter” on most U.S. keyboards.
  • / Forward slash won’t work the same.

The backslash tells Python that the next character isn’t a code symbol: it’s literally part of the quoted text.

You can include a new line in a string literal with “\n”

Try this:

print('apple\norange')

OUTPUT:

apple
orange

It doesn’t print “norange” but instead converts “\n” to a line break between the two words.

Concatenating Strings

Concatenating strings means combining them. You can combine strings using “+” operator to print multiple things together:

food = "cheese"
dessert = "cake"
print('I like to eat ' + food + dessert + '.') 

This is different than using commas to print multiple items. When you do it this way, you are giving the print() function one string instead of giving it several strings and allowing it to decide how to print them together.

Python doesn’t add extra spaces between the words in this case, but you can still include spaces in string literals to make it look right.