-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hope to have f-string like in python #67
Comments
There already is such a thing - although not with exactly the same format (for various reasons): julia> using StrFormat
julia> b=1.234567
1.234567
julia> f"number \{.3f}(b)"
"number 1.235" It has a number of extensions over Python f-strings, you can also do more C style formatting, or formatting based on type-specific defaults (which you can add types to, or change the formatting) C-style: julia> f"number \%.3f(pi)"
"number 3.142" Settable defaults, with keywords: julia> f"\%(pi*10000,20,:commas,:left)"
"31,415.926536 " This is what I've used for debugging for the past 8 years 😁 |
For syntax that follows Python f-strings directly, use PyFormattedStrings.jl: julia> f"number {π:.3f}"
"number 3.142" That package simply transforms the f-string syntax to calling the |
@aplavin Are you planning on supporting the rest of the Python formatting functionality, such as ^ (center justification), _ and , for group separators, # for alternate output format? It would be nice to have a complete drop in replacement for Python f-strings (my focus was on equivalent functionality, not equivalent syntax) |
I'm going to support whatever functionality is available in the
Is that what you mean? julia> f"{1234:#x}"
"0x4d2" But I'm not planning to implement any formatting myself in that package, this is what lets it remain lightweight and as reliable as base Julia. If there are missing features or bugs there – would be best to fix them in |
The other thing that's missing in PyFormattedStrings.jl is Python's |
Hm, I don't remember exactly why I didn't go with julia> fmtfunc = ff"abc {x:.2f} {y:d}"
#6 (generic function with 1 method)
julia> fmtfunc((x=1, y=2.2))
"abc 1.00 2"
julia> x = 2
2
julia> fmtfunc((y=2.2,))
"abc 2.00 2" It does require a macro for performance anyway, maybe the interface should be |
I suppose if you really wanted to have the syntax more like Python's ( |
StrFormat.jl doesn't give any usage documentation and says it's a "work in progress" and "I plan on changing it shortly" so it doesn't seem ready for users. Also, imho the syntax for this stuff is already pretty fiddly and hard to remember, so any divergence from established f-string syntax (of Python, Rust) needs to be significantly better for the user in order to justify learning a new thing. |
I do need to update the docs (it's no longer a work in progress, it's been stable for a long time now). |
That is present in JuliaString/Format.jl, i.e. julia> format("π = {:6.3f}, {:^8.4s}", pi, "🍕")
"π = 3.142, 🍕 " |
Hope to have f-string like in python
reference:
https://peps.python.org/pep-0498/
https://realpython.com/python-f-strings/
https://github.com/bicycle1885/Fmt.jl
small example:
Most important of all f-string enables a compact and expressive syntax which is clean, fast and fun to type.
The text was updated successfully, but these errors were encountered: