Python 3 `print` Function: Flexibility Gains

The `print` function in Python 3, backported by `python-future` (via `from __future__ import print_function`), replaced the less flexible `print` statement. This change introduced keyword arguments like `sep` and `end`, allowing for easier customization of output formatting and redirection to files.

The top 3

  1. Python 3 `print`'s `sep`: Customizing Output Separators: Python 3's `print()` function introduced the `sep` parameter, allowing developers to define custom separators between printed items (defaulting to a space). This enables flexible formatting with characters like hyphens ('-') for dates or commas (',') for lists, a significant upgrade from Python 2's fixed space separator requiring manual string concatenation.
  2. `print`'s `end` Parameter: Mastering Line Termination: The `end` parameter in Python 3's `print()` function offers precise control over the character sequence appended after the output, with `\n` (newline) as the default. This allows for printing multiple items on the same line or using custom endings, which was more cumbersome in Python 2 where a trailing comma was often used to suppress newlines.
  3. Redirecting `print` Output: The `file` Parameter: Python 3's `print()` function gained the `file` parameter, enabling straightforward redirection of output to any file-like object, such as a physical file or `sys.stderr`. This offers superior flexibility for logging and error reporting compared to Python 2's more specialized `print >> file_object` syntax.

Sources

Open the full topic