Skip to content
Related Articles

Related Articles

sys.stdout.write in Python
  • Last Updated : 01 Oct, 2020
GeeksforGeeks - Summer Carnival Banner

This is a built-in Python module that contains parameters specific to the system i.e. it contains variables and methods that interact with the interpreter and are also governed by it. 

sys.stdout

A built-in file object that is analogous to the interpreter’s standard output stream in Python. stdout is used to display output directly to the screen console. Output can be of any form, it can be output from a print statement, an expression statement, and even a prompt direct for input. By default, streams are in text mode. In fact, wherever a print function is called within the code, it is first written to sys.stdout and then finally on to the screen. 

sys.stdout.write() serves the same purpose as the object stands for except it prints the number of letters within the text too when used in interactive mode. Unlike print, sys.stdout.write doesn’t switch to a new line after one text is displayed. To achieve this one can employ a new line escape character(\n).

Syntax:

sys.stdout.write(<some string text here>)

Example 1:



Python3




import sys
  
  
sys.stdout.write('gfg')
Output
gfg

Example 2:

Python3




# script mode
import sys
  
  
sys.stdout.write('gfg')
sys.stdout.write('geeks')
sys.stdout.write('\n')
sys.stdout.write('for geeks')
Output
gfggeeks
for geeks

stdout can be also be used to print multiple elements. Not just this stdout can be assigned to another variable as long as it supports write().

Example 3:

Python3




import sys
  
# stdout assigned to a variable
var = sys.stdout
arr = ['geeks', 'for', 'geeks']
  
# printing everything in the same line
for i in arr:
    var.write(i)
  
# printing everything in a new line
for j in arr:
    var.write('\n'+j)

Output:

geeksforgeeks
geeks
for
geeks

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :