English
+370 5 205 5502 sales@monovm.com

How to use the print function in Python

In this article, you will understand how the print() function and print statements work in Python. It is a commonly used command to print the output to the console.

29 Apr, 22 by Susith Nonis 7 min Read

List of content you will read in this article:

Python is one of the significant used programming languages, from web developments to machine learning and whatnot. Due to its excellent online documentation, people use it a lot for web-based development. You can use its easy-to-understand syntax, making it commonly used among beginners. Several commands and syntax are available in Python to make your complex job easier. 

So cutting to the chase, in this article, we majorly focus on the print() syntax and how printing works in Python. We have mentioned several scenarios with examples for better understanding.

Learning how to print in Python should be the topmost priority to make your Python learning journey smoother. Python comes with a print() function for displaying the output to the console. The text could be a descriptive message, computational result, or error message for a more straightforward debugging process. The print function and its syntax may vary from version to Python.

If you are using Python2, you only have to use the print keyword on the console. For example,

print “Hello World.”

If you are using Python3, you need to use the print() function below.

print (“Hello World”)

You would get the error message below if you forgot to use the opening and closing parenthesis while using the print() function.

The only difference in the print syntax in Python 2 and Python3 is that Python2 has print statements while Python3 has a print function.

The print() function comes with several parameters with default values mentioned below. The actual print() function looks as below.

print(*object,sep=' ',end='\n',file=sys.stdout,flush= False)

Where,

  • Where object specifies the zero, one or more data to be printed and can be of any data type, these objects are first converted to the string before being printed.
  • The sep (separator) specifies how more than one object is separated. By default, the objects are separated by space.
  • The end (optional) will specify how the line will end. By default, the end parameter has a newline character.
  • File ( optional option) is an object allowing you to write and append text to a file. The default value of this parameter is sys.stdout, which will display the output on the screen.
  • Flush is a boolean parameter that will forcibly flush or buffer the stream and has a default value of false.

There is no return value for the print() function.

The print will display an empty line to the console without any parameters. If you do not pass any object or argument for the print function, you still have to include the parenthesis set for its working, and else you will end up in an error message. You can make it evident if you are using the Python REPL (Read Eval Print Loop).

For starting a new session after installing the Python3, you type exit() for ending the session. It works similar to hitting the enter key, creating a new line, and moving the cursor to the new line. The print() function works similarly without any parameters, and it displays an empty line on the console.

You need to pass the string literally as an argument to the print() function for printing the strings. You need to use either single or double quotes for the string literals. For example.

Also, you can use variables to store the string literal and pass that variable name as an argument to the print() function.

In such a way, you can provide the meaningful names of the variables as per the stored content. It improves the reliability of the code and allows other developers to understand the code better. 

As we have mentioned above, you can pass more than one object within the print() function. You can pass several parameters separated by the comma, as shown below.

While in the below example, we have passed two different types of objects, one is a string literal, and the other is the variable storing a string literal, for example.

You can also use the concatenation with the addition operator shown below to get the same result.

While using concatenation, you need to take care of the spaces, else you will end up with the wrong output, as shown below.

Also, always make sure that you cannot use the string literal with the number; otherwise, you will get the error message below.

If you still want to concatenate the string literal to a number, you need to convert it to a string literal while passing them as an argument. For example.

You can go for the modern printing of the objects by using an f-string, as shown below.

The type of arguments is limited to the strings. You can also pass objects with another data type. You can use all data types with the print() function.

As mentioned earlier, sep is used to determine the separator type in the print() function. By default, this parameter’s value is spaces. You need to provide some value explicitly to the sep parameter to change the default value, as shown below.

Also, you can even remove the spaces by simply providing the empty string, as shown below.

As we have mentioned above, the syntax of the print() function comes with the default parameter “end” with the default value of the newline character. A new line is created by default whenever the print() function is called. 

Suppose you call the trow print function separately, one after the other. In the below example, you will see that the second call is displayed on a new line, as shown below.

To change the default value of the “end” parameter, you can provide the value explicitly and change it to an empty string, as shown below.

You can also change it to a complete stop, as shown below.

Many people want to print the output of their command on the console, but sometimes it is required that the output of any command will be directed to a file. Make sure that the file to which you want to add the text should be an existing one. 

Suppose you have an existing text file to which you want to use the print() function to add some text. You need to open and write to a file to add the text. For that, you will require the open() function. You can pass the file name as the parameter along with the (-w) mode for specifying the write mode. 

Every time you run your code, the file’s contents will get deleted and replaced by new text. If you want to keep the existing text, you can use the (-a) mode for adding the text to the end of the existing one. 

You can add any text to the file and set its parameter to the placeholder name that you have created for the file, as shown below.

In this article, you will understand how the print() function and print statements work in Python. It is a commonly used command to print the output to the console. This command comes with several parameters with their default values. You need to provide explicit values and check the differences yourself to change those values. 

We have mentioned different scenarios and examples for your understanding. Happy coding!

People also read: 

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I veture into the world of Technology I try to share what I know in the simplest way possible. Not a fan of coffee, a sweet addict and a self accredited 'master chef'.