PyFormattedStrings.jl

Author JuliaAPlavin
Popularity
2 Stars
Updated Last
3 Months Ago
Started In
February 2024

Overview

Julia implementation of Python-like formatted string literals or f-strings. See PEP 498 for the original motivation and background for f-strings in Python.

PyFormattedStrings.jl mirrors the corresponding Python behavior as closely as possible without reimplementing formatting from scratch. Actual formatting is performed by the Printf stdlib, this package just converts the formatted string syntax. There is no additional runtime overhead compared to a manually-written corresponding @printf call.

Supports all f-string features that are directly available in Printf. Compared to Python, these are not implemented: (1) postfix modifiers (!a, !s, !r), (2) = and ^ alignment options, (3) = sign to show expression.

Examples

julia> using PyFormattedStrings
julia> x = 5.123

julia> f"{x}"
"5.123"

julia> f"value is now {x:.1f}"
"value is now 5.1"

julia> f"fraction of {(x-5)*100:d}%"
"fraction of 12%"

With the ff"..." syntax, the formatting string can be created ahead of time and applied to values later:

julia> fmtfunc = ff"value a={a:.2f} and first b={first(b):d}"

julia> fmtfunc((a=1, b=[2, 3]))
"value a=1.00 and first b=2"

julia> fmtfunc = ff"{re:.2f} {im:d}"

julia> fmtfunc(1.234 + 5.678im)
"1.23 6"