Subscribe Contact

Home  »  Chapter 2 : Programming Basics
Printing

Overview

The first step we will take to learn programming and Python is printing to the screen. We often think "printing" means to print on paper, but in our context, for now, we will print text, numbers and symbols on the computer screen. Printing on the screen is a simple form of output and allows us to quickly see the results of our programming efforts. Printing on the screen in this form is not graphical, that is, the screen will only display textual output (letters, numbers, and symbols). For now, this is sufficient to give us a way to learn the basics of Python programming. Later, after you have mastered the basics, you can explore Graphical User Interface (GUI) programming where you will be able to interact with graphics programmatically as well. Recognized by its name followed by parentheses, the print() function is built into Python, meaning that you can use it without any preparations or installations. Inside the parentheses, you place the information you wish to display, known as arguments, enclosed in quotes if you're dealing with text (e.g., print("Hello, World!")). The print() function is not limited to one type of data—it is versatile, allowing numbers, variables, and even complex expressions to be included as arguments. As you progress, you will find that the print() function is an invaluable tool in Python.

Concept: Input - Processing - Output

In computing we frequently use the terms input, processing, and output. Input is anything that is entered into the computer, such as characters typed on a keyboard, mouse clicks, taps on the screen, recording our voice, recording video, information from sensors, and many others. Processing is what happens to those inputs inside the computer, including calculations, storing values in memory or on hard drives, uploading input data to the Internet, etc. And output is the result of processing, including displaying results on the screen, printing on paper, playing video or sounds, etc.


Study Tip

Tip: Many pages in this eBook contain examples of code that you can study and try hands-on in your programming environment. I recommend you open you code editor, type the examples, run them and study the results to reinforce the discussion and examples throughout this book.

Common Use & Examples

Printing in Python

In Python we use the print() function to print alphanumeric characters (the alphabet, numeric digits, and other symbols) on the computer screen. The print() function is versitile and has a number of different ways we can use it.

Concept: Strings

In Python we call text strings. Strings are characters, words, or phrases and contain from one to any number of valid alphanumeric symbols surrounded by quotes (" "). Alphanumeric symbols include the letters of the alphabet, numeric digits (when included in a string, numbers are not numeric but characters of the string), all of the other symbols on the keyboard (~!@#$%^&*()_+=-`{}|[]\:";'<>?,./'`), and extended characters from non-English languages, etc.

Printing Text (Strings)

Try the following examples of printing strings in your editor:

Python Code:

print("Hello, World!")
print("Hello,", "World!")
print("Hello" + "," + " " + "World" + "!")
print("Hello", "World", sep=", ", end="!")
If you wrote these examples exactly the same in your environment and ran them, the output should look like this:

Output:

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Code & Output Details:
Study Tip

Using the examples above, I recommend practice by changing the string and string segments in various ways until you are comfortable with the forms of concatenation using the automatic comma spacing, the plus (+) sign, and the sep/end arguments.

Concatenation

String concatenation is a valuable technique that allows you to combine or join multiple strings. This operation is beneficial when creating longer or more complex strings. Python offers different ways to perform string concatenation. One of the simplest methods is using a comma (,) between strings. For example, if you have two strings, "Hello" and "world", you can concatenate them in a print statement like this:

Python Code:

print("Hello", "world")

Output

Hello world

Concept: Concatenation

Concatenation refers to the act of combining or linking things together in a sequence or series. In the context of programming or computer science, concatenation often specifically refers to joining strings together to create a single, long string. This is accomplished by placing one string after another, effectively merging their characters or contents. String concatenation is a commonly used when working with text or manipulating data in programming languages.

Another method of concatenation is using the plus (+) operator. For example, if you have those same two strings, "Hello" and "world", you can concatenate them using the expression "Hello" + "world", like this:

Python Code:

print("Hello" + "world")

Output

Helloworld
Notice the problem we have, though; using the + operator our two strings have no space between them, which is because the + operator does exactly what we tell it to do. In this case, we said put these two strings (words) together. Without any other instructions, we get exactly what we asked for.

What if what we wanted was this:

Python Code:

Hello, world!
The word Hello is followed by a comma and a space and then the word world followed by an exclamantion point? We have several options to do this. Here is the code for several approaches and then an explanation of each:

print("Hello" + ", world!")
print("Hello" + ", " + "world" + "!")
print("Hello", end=", ")
print("world!")
print("Hello", end=", ")
print("world", end="!")
print("Hello", "world", sep=", ", end="!")

Output

Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!

Code & Output Details


Printing Numbers


Next, try the following examples of printing numbers in your editor:

Python Code:

print(10)
print(10, 20, 30)
print(10 + 20 + 30)
If you wrote these examples exactly the same in your environment and ran them, the output should look like this:

Output:

10
10, 20, 30
60

Code & Output Details:


Printing Strings & Numbers Together


We can also combined printing strings and numbers together in a single print() function statement. The following examples demonstrate how we might combine strings and numbers in print() function statements.

Python Code:

print("Age:", 21)
print("Age: " + str(21))
print("Sum: " + str(10 + 20 + 30))
If you wrote these examples exactly the same in your environment and ran them, the output should look like this:

Output:

Age: 21
Age: 21
Sum: 60

Code & Output Details:

  • Code Line 1: This line demonstrates using the print() function to print a string and a number. In this case, the two values are separated by a comma, which as we saw previously, automatically adds a space between the two objects. In addition, the comma in this case also separates the fact that the two objects of differt types of data (a string and an integer). The print() functioncan print them in this form to look like they are together even though they are actually separate in the code.
  • Code Line 2: On this line, we want to concatenate the label Age with the integer value to make one single string. In this case, we must use the casting function str() to convert the number value to a string so that it can be concatenated with the label Age.
  • Code Line 3: As we saw previously, we can perform calculations in print() function statements. In this case, because we are also tring to concatenate the resulting numeric calculated value with the label Sum, we need to use casting here as well.
Concept: Casting

In programming, we often need to convert data from one type to another type. For example, in Code Lines 2 & 3 of this Python code ...

print("Age: " + str(21))
print("Sum: " + str(10 + 20 + 30))


... requires us to cast (convert) the numeric values to strings so that we can concatenate those values with our string labels Age and Sum. We do this, in this case, using the str() function, which casts numeric values to string values. The process of converting (casting) data from one type to another is called casting.


Print Function General Form

Every function has a general form. It is important to learn how to interpret general forms. The general form shows all of the options available for any given unit of syntax in a programming language. For example, here is the general form of the print() function:

print(object(s), sep=separator, end=end, file=file, flush=flush)

print() is the function name, and everything inside the parenthesis ( ) are the function's arguments. The following table describes each argument in the general form of the print() function:

Concept: Arguments

In programming, we often pass data and objects between different segments of our programs. For example, when you call a function, like print(), you can often pass values to that function for it to perform its tasks. These values that we pass are called arguments.

Argument Description
object(s) Any objects in Python and any number of objects. Objects that are not a string of alphanumeric characters are converted to alphanumeric equivalents for printing. Here is an example of a print function call with two objects as arguments. For example:
print("Hi", "Bob")
The output of this print function call would be Hi Bob. Notice the space between "Hi" and "Bob". The reason for this is explained in the sep=separator argument described below.
sep=separator Optional argument. When you specify more than one object to print in one argument list, you can also specify how those objects are separated when printed. The default is a blank space " ". You could change this to any other one or more characters by indicating it here in the second argument of the function call. For example:
print("Hey", "What's up?", sep=", ")
This print function statement has two object arguments: "Hey" and "What's Up?", and the sep=", " indicates that when this statement runs, it should print Hey, What's up? Notice that the sep=", " includes a comma and a space after it so that when the two objects print, they will be formatted properly with a space between the comma and the next word.
end=end Optional argument. You can use this argument to append (add) one or more characters to the end of the output. For example:
print("Hi", "Bob", end="!")
This print function statement has two object arguments, "Hi" and "Bob", and it includes end="!" which results in the output: Hi Bob!. We did not have the sep argument this time, so we got the single default space " " as the separator between the two objects.
file=file Optional argument. This argument is used with objects a bit more complex than simple alphanumeric strings of characters. We will revisit this argument later in the book.
flush=flush Optional argument. This argument is used to manipulate how data is handled internally, which is a bit more complex of a concept for now. We will revisit this argument later in the book.


Practice Problems

Read the following programming prompts and write a program that solves the problem in your programming environment. Do not read the Solution until you try to solve it yourself first. Once you have completed writing your code, run your program, click the Solution button and compare your work.

Note: There is often more than one way to write solutions in programming. Your solution may not match the solution provided below exactly, which is generally fine if you're getting the required results. Use the provided solution as an example of an approach to learn from while not interpreting it as the only "right" solution.

Problem 1

Write a Python program using the print() function to output the following contact information. Your output should match the format and lines shown here:

Bob Smith
1234 Nowhere Street
Someplace, UT 84999
(801)555-8888
bob@smithco.com



Problem 2

Write a Python program using the print() function with two object arguments and also use the sep and end arguments to print the following output:


Wow, that's great news!



Problem 3

Write a Python program using the print() function with three object arguments to construct a (fictitious) social security number. The three object arguments should be the three sets of numbers that make up an SSN, like 123-45-6789. Also, use the sep argument to separate the three sets of numbers with a dash symbol. Here is an example of the output:


111-22-3333




«  Previous : Overview of Python
Next : Text (a.k.a. Strings)  »



© 2023 John Gordon
Cascade Street Publishing, LLC