gh-141395: Clarify stdout flush behavior for newline characters in print()
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/