Home » Chapter 2 : Programming Basics
Printing
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.
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.
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:
Hello, World!
Hello, World!
Hello, World!
Hello, World!
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:
print("Hello", "world")
Hello world
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:
print("Hello" + "world")
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.
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="!")
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
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:
10
10, 20, 30
60
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:
Age: 21
Age: 21
Sum: 60
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:
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:
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:
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:
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. |
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.
Write a Python program using the print() function to output the following contact information. Your output should match the format and lines shown here:
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:
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: