Kodeclik Blog
Python print()
Python’s print() is a versatile function that provides various options to format and style the output from your programs.
How the Python print() function works
The first thing to note about Python’s print() is that it is a function which means you need to pass it arguments. For instance:
print(“Hello, how are you?”)
This yields the output:
Hello, how are you?
If we do:
print("Hello, how are you?")
print("I am ok")
We get:
Hello, how are you?
I am ok
Note that the second print() command prints its output on a new line. Calling print() without any arguments yields a blank line. For instance, the following segment of code:
print("Hello, how are you?")
print()
print("I am ok")
Hello, how are you?
I am ok
Printing multiple information elements in print()
We can create a composite statement by printing a combination of strings using the string concatenation (+) operator.
name = "John"
year = "10"
print(name + " is " + year + " years old.")
This yields the output:
John is 10 years old.
Note that we have included extra spaces around “is” and before “years old” in order to obtain the formatted string. (See also our detailed blogpost on the Python concatenate operator.)
Note carefully that the variable year has been defined as a string. If you try it like this:
name = "John"
year = 10
print(name + " is " + year + " years old.")
You will get the error:
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(name + " is " + year + " years old.")
TypeError: can only concatenate str (not "int") to str
The reason for the error is that the concatenation operator requires strings on both sides of the operation. There are two ways to fix this. One is to convert the integer into a string using the str() function.
name = "John"
year = 10
print(name + " is " + str(year) + " years old.")
Another is to keep the year as integer and instead use print() as follows:
name = "John"
year = 10
print(name + " is ", year, " years old.")
This will yield the output:
John is 10 years old.
Note that there are extra spaces included if we use year as a separate argument in the print() function. We should therefore update the program as:
name = "John"
year = 10
print(name + " is", year, "years old.")
Separators in Python print()
When you use the multiple argument version of print() there are additional options available. For instance, you can do:
student1 = "John"
student2 = "Kathy"
student3 = "Ryan"
print(student1,student2,student3,sep="--")
This gives:
John--Kathy--Ryan
The sep argument denotes the separator. In this case we are telling print() to use the string “--” as a separator (instead of the default single space). So if you wanted to mimic the effect of the “+” operator, you make sep to be an empty string.
student1 = "John"
student2 = "Kathy"
student3 = "Ryan"
print(student1,student2,student3,sep="")
This gives:
JohnKathyRyan
Using separators is particularly useful if you are printing your output to a file that will later be read by a program like Excel so that when importing the file you can specify the separator.
The end argument in Python print()
Just like the separator argument that specifies what should be printed *in between* the elements, there is an argument to specify what should be printed at the end. Recall that every print() statement starts on a newline. From this you can infer that the default end character is a newline. We can change it like so:
print("This will be on a line by itself")
print("This is the second line",end='')
print("This is also the second line")
This gives:
This will be on a line by itself
This is the second lineThis is also the second line
Here is a place where this is useful. Suppose we would like to print all the months in a year with commas between them. One way to do it would be:
months = ['January','February','March','April','May','June',
'July','August','Sep','October','November','December']
for i in months[:-1]:
print(i,',',sep='',end=' ')
print(months[-1] + '.')
Note that, inside the for loop, we are printing all months except the last with an empty string as a separator (between the month and the comma), and then a space as an end character (before the next month is printed in the next print() statement inside the loop). Finally, we print the last month with a period at the end. This program uses all the elements we have discussed so far.
For more interesting content on Python printing, learn how to print special characters using the Python chr() function.
Also checkout our blogpost on printing lists in Python.
Interested in more things Python? See our blogpost on Python's enumerate() capability, how to print to stderr, and how to read a text file in Python. Also learn all about computing square roots in Python and master recursion in Python!
Want to learn Python with us? Sign up for 1:1 or small group classes.