orin xprint xy is equivalent to the longerimport sys sys stdout write(str(xstr( '\ 'which manually performs string conversion with stradds separator and newline with +and calls the output stream' write method which would you rather code(he sayshoping to underscore the programmer-friendly nature of prints obviouslythe long form isn' all that useful for printing by itself howeverit is useful to know that this is exactly what print operations do because it is possible to reassign sys stdout to something different from the standard output stream in other wordsthis equivalence provides way of making your print operations send their text to other places for exampleimport sys sys stdout open('log txt'' 'print(xyxredirects prints to file shows up in log txt herewe reset sys stdout to manually opened file named log txtlocated in the script' working directory and opened in append mode (so we add to its current contentafter the resetevery print operation anywhere in the program will write its text to the end of the file log txt instead of to the original output stream the print operations are happy to keep calling sys stdout' write methodno matter what sys stdout happens to refer to because there is just one sys module in your processassigning sys stdout this way will redirect every print anywhere in your program in factas the sidebar "why you will careprint and stdouton page will explainyou can even reset sys stdout to an object that isn' file at allas long as it has the expected interfacea method named write to receive the printed text string argument when that object is classprinted text can be routed and processed arbitrarily per write method you code yourself this trick of resetting the output stream might be more useful for programs originally coded with print statements if you know that output should go to file to begin withyou can always call file write methods instead to redirect the output of print-based programthoughresetting sys stdout provides convenient alternative to changing every print statement or using system shell-based redirection syntax in other rolesstreams may be reset to objects that display them in pop-up windows in guiscolorize then in ides like idleand so on it' general technique automatic stream redirection although redirecting printed text by assigning sys stdout is useful toola potential problem with the last section' code is that there is no direct way to restore the original output stream should you need to switch back after printing to file because assignmentsexpressionsand prints