Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Are there situations in which sys.stdout.write() is preferable to print?

(Examples: better performance; code that makes more sense)

1 Answer

0 votes
by (106k points)

The print function first converts the object to a string (if it is not already a string). The print function will also put a space before the object if it is not the start of a line and a newline character at the end.

When you use stdout, that time you need to convert the object to a string by yourself and you will do it by calling "str",  and there is no newline character.

So the below-written print() statement is equivalent to what we have done using sys.stdout():-

print(10)

import sys

   sys.stdout.write(str(10) + '\n')

Related questions

0 votes
4 answers
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)

Browse Categories

...