Advanced Print Techniques

Keyword Arguments

Print accepts several keyword arguments:

  • sep - Separator between values
  • end - String appended after the last value
  • file - Output stream (default is sys.stdout)
  • flush - Force buffer flush

Print Streams

You can redirect print output to different streams:

  • sys.stdout (default)
  • sys.stderr (for errors)
  • File objects
  • StringIO objects

Format Specifiers

Advanced string formatting with format specifiers:

  • %s - String
  • %d - Integer
  • %f - Float
  • %.2f - Float with 2 decimal places
  • %x - Hexadecimal

F-strings

Modern Python uses f-strings for elegant formatting:

  • Basic expressions
  • Method calls
  • Format specifiers
  • Multiline support