What does print >> do in python?


In Python 2, there was an alternative syntax for using the print statement for printing that involved using the >> operator, also known as the right shift operator. However, this syntax has been deprecated and removed in Python 3. Therefore, if you see code that uses print >> syntax, it is likely written in Python 2 and will not work in Python 3.

The correct syntax in Python 2 for redirecting the output of the print statement to a file-like object is using the print statement followed by the >> operator and the file object.

Here are some code examples that demonstrate the usage of the >> operator with the print statement in Python 2, along with step-by-step explanations.

Redirecting Print Output to a File

Example

In the example discussed below, the print statement is used with the >> operator to redirect the output to the myfile.txt file. The content within the parentheses is the message we want to print. By using the >> operator followed by the file object (file_obj), the output will be written to the file instead of being displayed on the console.

Assuming we have a text file myfile.txt as follows

#myfile.txt
This is a test file

#rightshiftoperator.py
# Open a file for writing
file_obj = open("myfile.txt", "w")

# Redirect the output to the file using the >> operator

print >> file_obj, "Hello, World!"

# Close the file
file_obj.close()

When the above code is run with python2 exe as $py -2 rightshiftoperator.py we get the following

Output

Hello, World!

Redirecting Print Output to Standard Error

Example

In the example below, the print statement is utilized with the >> operator to redirect the output to the standard error stream (sys.stderr). It so happens that the content within the parentheses is the error message we wish to print. By using the >> operator followed by sys.stderr, it will be seen that the output will be directed to the standard error stream instead of the standard output.

import sys
# Redirect the output to the standard error using the >> operator
print >> sys.stderr, "Error: Something went wrong!"

Output

Error: Something went wrong!

Redirecting Print Output to a Network Socket

Example

Here, as seen in this example, the print statement is made use of with the >> operator to redirect the output to a network socket. The content, as it is seen here, within the parentheses is the data we would wish to send to the server. By utilizing the >> operator followed by the socket object (sock), the output will be eventually sent to the server through the network socket connection.

import socket
# Create a socket connection to a server
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("localhost", 8080))

# Redirect the output to the network socket using the >> operator
print >> sock, "Data: Hello, Server!"
# Close the socket connection
sock.close()

Output

Data: Hello, Server!

Redirecting Print output in Python 3

In Python 3, it can be found that you can achieve the same effect of redirecting the output of the print() function to a file using the file parameter.

Example

In the example given below, the print() function is used with the file parameter to specify the file object (file_obj) where the output should be redirected and written. The content within the parentheses, is intended that, is the message we want to print. It is observed that by passing the file parameter with the file object, the output will be re-directed to the file specified instead of being displayed on the console.

Assuming we have a text file myfile.txt as follows

#myfile.txt
This is a test file

# Open a file for writing
file_obj = open("myfile.txt", "w")

# Redirect the output to the file using the file parameter
print("Hello, World!", file=file_obj)

# Close the file
file_obj.close()

Output

Hello, World

I hope these examples helped you understand the usage of the right shift operator or the >> operator with the print statement in Python 2.

It's also important to note that these examples are specific to Python 2 environment and won't work in Python 3. In Python 3, you should use the print() function with the file parameter to achieve similar functionality.

It's also significant to realize that in Python 3, the print() function is a regular function and not a statement. Therefore, it requires parentheses to be used with the print function even if no arguments are provided.

Updated on: 13-Jul-2023

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements