print and format on steroids
pip install say==1.6.6
It's been almost fifty years since C
introduced printf()
and the basic
formatted printing of positional parameters. Isn't it time for an upgrade?
You betcha!
say
evolves Python's print
statement/function, format
function/method, and %
string
interpolation operator with simpler, higher-level facilities. For example,
it provides direct template formatting:
from say import * x, nums, name = 12, list(range(4)), 'Fred' say("There are {x} things.") say("Nums has {len(nums)} items: {nums}") say("Name: {name!r}")
yields:
There are 12 things. Nums has 4 items: [0, 1, 2, 3] Name: 'Fred'
Or instead of printing the string immediately, you want the string itself,
the fmt()
function is our version of sprintf()
.:
>>> fmt("{name} has {x} things and {len(nums)} numbers.") 'Fred has 12 things and 4 numbers.'
The more items being printed, and the more complicated the format
invocation, the more valuable this simple inline specification becomes.
But as nice as replacing positional templates with inline
templates
is--a fact finally officially accommodated with
formatted string literals of Python 3.6--say
does a lot more.
It also works in a variety of ways to up-level the output-generation task.
For example:
say.title('Discovered') say("Name: {name:style=blue}", indent='+1') say("Age: {age:style=blue}", indent='+1')
Prints a nicely formatted text block, with a proper title and indentation, and just the variable information in blue.
say
provides:
format()
method, syntax, and underlying engine.fmt()
object for string formatting.Take it for a test drive today! See the full documentation at Read the Docs.