The Wayback Machine - https://web.archive.org/web/20240602163602/https://www.geeksforgeeks.org/sys-stdout-write-in-python/
Open In App

sys.stdout.write in Python

Last Updated : 01 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

Python - sys.stdout.flush()
A data buffer is a region of physical memory storage used to temporarily store data while it is being moved from one place to another. The data is stored in a buffer as it is retrieved from an input device or just before it is sent to an output device or when moving data between processes within a computer. Python's standard out is buffered. This m
3 min read
Python | Testing Output to stdout
Testing is a critical part of development as there is no compiler to analyze the code before Python executes it. Given a program that has a method whose output goes to standard Output (sys.stdout). This almost always means that it emits text to the screen. One likes to write a test for the code to prove that, given the proper input, the proper outp
2 min read
How to print to stderr and stdout in Python?
In Python, whenever we use print() the text is written to Python’s sys.stdout, whenever input() is used, it comes from sys.stdin, and whenever exceptions occur it is written to sys.stderr. We can redirect the output of our code to a file other than stdout. But you may be wondering why one should do this? The reason can be to keep a log of your code
3 min read
Python - sys.settrace()
Python sys module provides some of the powerful functions but they are complex to understand. One of which is the sys.settrace() which is used for implementing debuggers, profilers and coverage tools. This is thread-specific and must register the trace using threading.settrace(). Knowing how the function works internally is really important in case
3 min read
Python | sys.getallocatedblocks() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getallocatedblocks() is used to get number of memo
1 min read
Python | sys.getrecursionlimit() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getrecursionlimit() method is used to find the cur
1 min read
Python | sys.getdefaultencoding() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getdefaultencoding() method is used to get the cur
1 min read
Python | sys.setrecursionlimit() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.setrecursionlimit() method is used to set the maxi
2 min read
Python | sys.setswitchinterval() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.setswitchinterval() method is used to set the inte
2 min read
Python | sys.getswitchinterval() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getswitchinterval() method is used to get the inte
1 min read
Article Tags :
Practice Tags :