0 votes
1 view
in Python by (47.8k 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 (107k 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
1 answer
0 votes
2 answers
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...