Print

When we code, we often print into our console to see what information we are working with and receiving. This is possible using the print function. An example of this is below:

print("Hello World")

This code will print the words: Hello World into the console. Try this in your own code editor of choice and change the values in the quotation marks to see how the printed value changes!

This print function can do more than just print a value. We can give it multiple values and it'll handle printing those values in certain ways. We can also change how it behaves after printing a value. These are arguments that we pass into the print function. While we won't dive deep into what these arguments conceptually are yet, since that will happen in Chapter 5 when we look at Functions, we will look at how to do this and what change it has on the output.

sep

The first additional argument is the sep argument. First, try changing the print code to run the following code snippet:

print("Hello", "World")

Upon running this, notice that it prints Hello World with a space in between the two words. Now, if we update the print statement to the code snippet below, pay attention to what differences occur.

print("Hello", "World", sep=',')

Did you notice that now we have Hello,World getting printed? Try adding more values to the print statement and see how that changes the output. For example, try the following!

print("Hello", "World", "I'm", "learning", "to", "code", sep=',')

Now, try changing the value between the single quotes after the sep= to something besides a comma. Try a period and see what that does! Notice that as we add more values, our print will print out those values separated by the character provided in those quotes.

end

Now that we've looked at the first argument of sep, let's look at the argument of end. Continuing this update of print, let's change our print statement to the following snippet:

print("Hello", "World", end='.')

Upon running this code, note that the new line is gone. In the previous code snippets above, once we ran our code, a new line would be added after the text. This would put our input on the next line. However, upon changing the value of end to a period, that new line is gone. We're now on the same line as our output. The output should look like this:

>> python3 hello.py
Hello World.>>

This is useful as the desired behavior may not be to include a new line after the content is printed. We will see more useful cases of these later in Chapter 4 when we discuss For Loops and printing different information on the same line or on different lines. In order to keep this section simple, another simple example will be provided below. What if we wanted to print the text without anything after the printed after? We could write the following snippet to accomplish this:

print("Hello", "World", end='')

Upon running this code, our output would look like this:

>> python3 hello.py
Hello World>>

Combining end and sep

Now that we've looked at both additional arguments in separation, let's look at them together. If we wanted to create the following output: "John,Doe,+1 (415) 001-2020,3 Main Street.", we could simply write the following print statement.

print("John", "Doe", "+1 (415) 001-2020", "3 Main Street", sep=",", end=".")

This may not seem the most practical usage, but when we discuss csv files in Chapter 12, this may be more useful.

Conclusion

In conclusion, when we want to show the user information in the console, we would use the print function. The print function can take in as many values separated by commas. Additionally, the print function has two additional arguments. The first is the sep argument, while the second is the end argument. The sep argument allows the programmer to change the separating value between each piece of information printed out. Without overriding the sep argument, python will naturally add spaces between each value. The end argument allows the programmer to change the behavior after the console finishes printing the line. Without overriding the end argument, python will naturally add a new line after printing the line. These are useful arguments to customize the behavior of the text being printed, formatting the information in certain ways that may be more fitting than the default. These two arguments don't have to be overridden if a space is the better separator for the situation and a new line is the expected behavior after printing the line.