Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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
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)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...