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.
Integers with Python print function
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.
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.
Concatenate strings
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.
Concatenate strings and integers
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.
Parsing numbers to strings
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.
Different data types with the Python print function
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 Python print function with lists
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.
Dictionaries with print() function
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.
Printing out strings
Maybe we may want to print out a string to tell us what kind of information 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.
Variables with print() function
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.
Using f strings
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.
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.
Printing out a dictionary
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.
Summary
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.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
