Python print() Function: The Complete Guide

0
146
Python print() Function Example

Python print() function prints the given object to the standard output device (screen) or the text stream file.

Python print()

Python print() is a built-in function that converts an object you want to print to the screen into a string, then prints that object. To get the output in a standard output device ( like Monitor ), then the most straightforward way in Python is to use the print() function.

Syntax

See the following syntax.

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

Parameters

There are some parameters, as we can see on the above syntax:

  • Object: Object is that thing that is to be printed. There may be many objects in a single print() function. That’s why we have used * before an object.
  • sep: This sep means the objects which to be printed can be separated by a separator like a comma(,), space (”), etc. However, that default value of sep is ”.
  • end: This specifies what is to be printed at the end of the statement. The end value is ‘\n’ (next line).
  • file: This must be an object with the write method. The default value is sys.stdout, which means the output will be printed on the screen.
  • flush: This is a boolean value. If the value is True, the stream is forcibly flushed. The default value is False.

However, only the object parameter is required, and the other three are optional.

Return value

The function does not have any return value.

Example

See the following example.

# app.py

s = "Hello welcome to AppDividend"
# This will print the value of object s
print(s)

num = 10
# Usages of sep
# This will print the message and the value by a space
print("Value of the number is", num, sep=' ')

# Usages of sep and end
num1 = 20
print("Value of num is", num, sep=' ', end="\n\n")
print("Value of num1 is", num1, sep=' ')

Output

➜  pyt python3 app.py
Hello welcome to AppDividend
Value of the number is 10
Value of num is 10

Value of num1 is 20
➜  pyt

Here on the above code, we can see that when we use sep = ”, it separates two objects by a space and then prints it.

Also, we can see, we have used end=’\n\n’, which means there will be two next-line gaps between two print statements as per the above python code.

Python print() with file parameter

See the following code.

# app.py

# Opening a file in write mode
myfile = open('demo.txt', 'w')
# This message will be written to the file
msg = "Hello there"

# Printing the message to demo.txt
print(msg, file=myfile)

# Closing the file
myfile.close()

This program tries to open the demo.txt in writing mode. If this file doesn’t exist, the demo.txt file is created and opened in writing mode.

Here, we have passed the myfile file object to the file parameter. Then, the string object ‘Hello there’ is printed to the demo.txt file (check it in your system).

So, we have opened a file name demo.txt, and if this file does not exist in the current directory where the source code file is located, it will be created.

After that, the message “Hello there” will be printed ( in write mode) to the file.

If you do not know how to read and write a file in Python, then check out these two tutorials.

  1. How to read a file in Python
  2. How to write a file in Python

Finally, the file is closed using the Python close() method.

That’s it for this tutorial.

Related Posts

Python repr()

Python property()

Python pow()

Python iter()

Python next()

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.