Best way of using the Python Print Function

  • Reading time:6 mins read

What is the Python Print Function?

Print is a common built-in python function that prints out any specified message to any standard output device. In most cases, the print function is used to print out something to the terminal or the console for one to see.

The print function can also be used to perform some basic form of logging instead of using the basicConfig method.

 

 

How to use the Python Print Function?

In its simplest form, the print function is applied by typing the word print then followed by a pair of parentheses where we specify what we intend to print out. For instance, if we want to print out some string we need to wrap it around quotation marks. As shown here.

 

 

We can also print out an integer. However, in this, we need not wrap it around quotation marks as in the case of a string.

 

 

Python Print function with different data types

The Python print function can be used to work with different data types apart from integers and strings. We can also print out a variable, in programming a variable is a reserved memory location to store values. 

 

 

So in this scenario, the print function basically takes whatever value is stored in the variable and prints it out to the screen.

 

python print

 

Using Python Print function with strings

The print function can also be used to concatenate two strings together. Now, the easiest way to do this is by using the addition operator. However, the arguments must be strings.

To illustrate this we need to create another string representing the type of elephant. We can print both of these together by simply using a plus sign as shown below.

 

 

When we use the addition operator the two strings appear as one string in the output since we did not specify a separator. If we want a space between the two strings, we need to specify a separator and in this case, the separator will be an empty space.

 

 

This is going to put space between the two strings.

It is important that we note that we cannot concatenate a string and an integer. Now notice that if we create another variable age = 15, and try to concatenate it with the variable animal we will get a Type Error.

 

 

This is because we cannot concatenate a string and a number. However, there is a way around this, we can get rid of this error by converting the number to a string using the str() function. In Python, the str() function returns a string version of an object.

 

 

We can also print out other data types a well. For example, we can also print a list in its entirety. This is going to print out the whole list in the terminal.

 

 

Using the print function to print out certain parts of the list by using square brackets and then the number of the index of the item that we want to print out.

Python is a zero indexing language so the first item in the list is always represented by index zero.

 

 

So the second item will be a tiger in this case.

 

 

The print function can also be used with dictionaries. Just like in the case of lists we can also print out the whole dictionary.

 

 

And likewise, we can also index a dictionary by accessing all the keys so we can print out the value of the key. So quite often you might find that you want to print out some data and something else.

 

python print

 

Maybe we may want to print out a string to tell us what kind of information that we are looking for. To do that instead of printing out one thing at a time we can print out a string at the end of another string by adding a comma then a space and then the variable name.

 

 

We can also print out information by inserting a variable, a list or a dictionary in the middle of a string. One way of doing this is by adding the animal variable together with a plus sign.

 

 

Now fortunately Python has a great way of doing this and that is by using f strings. Now strings were introduced after Python 3.6. To use f strings just type the letter f before the quotation marks enclosing the string.

 

python print

 

If we need to print a variable within an f-string we simply need to do curly braces and pass in the variable name which is animal in this case. 

 

 

This method can be used with any kind of variable. For instance, we can pass in a dictionary within the curly braces.

 

 

The f string can also be used to pass in multiple variables within a string without the need to worry about concatenation or mixing strings and integers.

 

 

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.