cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-141395: Clarify stdout flush behavior for newline characters in print()

Open Vemulakonda559 opened this issue 1 month ago • 1 comments

This PR adds a note to the print() documentation to clarify how Python’s stdout buffering works with newline (\n) characters inside a single print call.

Motivation:

Current documentation mentions that flush() is implied for writes containing newlines.

However, it does not explain that Python flushes only after the entire write operation, not mid-string.

This can confuse users coming from C, who expect a flush at each newline, and developers writing scripts that rely on immediate output for progress indicators or CLI feedback.

What’s added:

A .. note:: block explaining that stdout behavior depends on the environment (TTY vs redirected stdout).

Guidance on explicitly flushing with flush=True or sys.stdout.flush().

Mention of python -u for unbuffered output.

A short example demonstrating the behavior.

Impact:

Improves clarity for learners and developers.

Aligns documentation with actual behavior across different environments.

Not a behavior change — documentation-only PR.

Related Issue:

Addresses issue #141395

  • Issue: gh-141395

📚 Documentation preview 📚: https://cpython-previews--142094.org.readthedocs.build/

Vemulakonda559 avatar Nov 29 '25 16:11 Vemulakonda559