Are there situations in which sys.stdout.write() is preferable to print?
(Examples: better performance; code that makes more sense)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
printis just a thin wrapper that formats the inputs (modifiable, but by default with a space between args and newline at the end) and calls the write function of a given object. By default this object issys.stdout, but you can pass a file using the “chevron” form. For example:See: https://docs.python.org/2/reference/simple_stmts.html?highlight=print#the-print-statement
In Python 3.x,
printbecomes a function, but it is still possible to pass something other thansys.stdoutthanks to thefileargument.See https://docs.python.org/3/library/functions.html#print
In Python 2.6+,
printis still a statement, but it can be used as a function withUpdate: Bakuriu commented to point out that there is a small difference between the print function and the print statement (and more generally between a function and a statement).
In case of an error when evaluating arguments: