Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

sys.stdout — the standard output stream

sys.stdout is Python's text stream for standard output, used by print() by default. It supports direct text writes and buffer flushing; it does not necessarily point to a terminal.

Version: Python 3.14; repository image, production runtime not attested

Syntax

import sys
sys.stdout.write(text)
sys.stdout.flush()

sys.stdout is a stream object, not a function. It is the default destination for print(). Do not confuse it with sys.stderr, where the interpreter sends error messages. The stream can be replaced with another file-like object, so do not assume it always represents a terminal.

sys.stdout.write(text) accepts str, not bytes, and returns the number of characters written. It does not append a newline automatically: use \n to end a line. Text encoding depends on the stream configuration.

By default, the standard sys.stdout is line-buffered during interactive use and block-buffered otherwise. To make standard output unbuffered, run Python with -u or set the PYTHONUNBUFFERED environment variable to 1. In CPython 3.14, the value 0 does not disable buffering. Python ignores this variable when started with -E or -I. sys.stdout.flush() flushes the write buffer. The example makes two writes that form one line, then explicitly flushes the buffer.

To write bytes, you can use sys.stdout.buffer if that attribute exists. A replacement object such as io.StringIO may not provide it. In some environments without a console, sys.stdout can be None; library code should not assume it is available.

Examples

import sys

sys.stdout.write("Hello")
sys.stdout.write(" world!\n")
sys.stdout.flush()

Result

Hello world!

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources