Printing

Recall the input and output lesson. There, I talked about how there are multiple kinds of input and multiple kinds of output. The first kind of output you will learn how to produce in python is text-based output. About the simplest possible program you can write in python is one that prints out some word(s) to standard output. The standard output of a program is an output stream that words and other text can be printed to by a program, and then displayed on the screen by your IDE.

print('the big black box')

Try opening up your IDE of choice, create a file named text.py, write this code, and then hit the run button. You should see something similar to the following:

IDE

You’ve now written and run a real python program! As exciting as this is, the program itself is not particularly amazing. The only thing that this does is print out 4 words. If you ran this program 100 times, it would just do the same thing over and over again. What this program accomplishes for us is not particularly complex. It will take time, and many lessons, to build the skills necessary to start building programs have more complexity and greater practical use for everyday life, but we have to start somewhere!

Let’s study this simple program in a bit more depth. The first part of the line, print, represents the name of a function that is built-in to python that we are invoking. As the name suggests, this function’s purpose is to print out text to the output of the program. Throughout your journey learning python, you’ll learn about many other types of functions, so stay tuned. After the print come a pair of parentheses, with some text inside. If you want a function to be invoked (to do its job) you have to put a pair of parentheses after it. This tells python to call this function. For some types of functions, we don’t have to put anything between these parentheses, but for this we can put stuff in it, which tells the function what to print. In this case, we put text, enclosed within single-quotes, to indicate to print these four words.

In python, text enclosed within quotes is referred to as a string. Strings are a very important concept in programming, and will come up often in these lessons. Strings are essentially a way for us to represent text (words, names, addresses, quotes, etc) within a program. String can be specified with either single-quotes or double-quotes. For example, try changing your program to this instead:

print("the big black box")

When you hit run, it should print the exact same thing as before. Why? Because python understands that text surrounded by either type of quote as a valid string.

What happens when we try to use this print function multiple times in one single program? Take a look at this python code:

print("the")
print('big')
print("black")
print('box')
What will the program above put in the standard output box when run?
the
big
black
box
Correct. The print function puts a newline character after the printed content by default, causing each word to be displayed on it's own line.
the big black box
Incorrect. The words would not display together on the same line as shown here.
the big
black box
Incorrect. This result shows two words on the first line and two on the second, which is not how the output text is formatted.

As you should see by now, it prints out each of the four words on its own line, so the output is slightly different than before. This is because by default, the print function adds a special character at the end of each string sequence that it prints, called the “newline” character. The newline character represents a line break. You can think of it as the equivalent of hitting the ENTER or RETURN key while writing a word document. Basically, it puts you on the next line.

You can actually insert your own newlines into a string wherever you want, by writing \n. Shown below are three variants of “the big black box” program. Notice where I have added newlines in the code. Try running these and pay attention to what is going on.

print("the\nbig\nblack\nbox")
print('the big\nblack box')
print("the")
print("big \n\n black")
print("box")

We can also give the print function a special directive to not put a \n at the end of the print by default:

print("the", end=' ')
print('big', end=' ')
print("black", end=' ')
print('box', end=' ')

In this program, I tell the print function to put a space (' ') rather than a newline ('\n') at the end instead. Now you know the basics of how to print things out in Python. Excellent! Feel free to stay on this lesson and play around with various simple printing programs in your IDE to get more comfortable with prints, strings, and newlines. When you’re ready, move on to the next lesson.



PyFlo Home Complete Bookmark Next