Python string formatting

196 views
Skip to first unread message

manda...@gmail.com

unread,
Oct 31, 2018, 1:13:34 PM10/31/18
to Django developers (Contributions to Django itself)
Hi,

Long story short, I discovered [1] there is no concensus [2] on which formatting to use in the Django tutorial.

An argument for % (or against the format method) was https://savannah.gnu.org/bugs/?30854 but it has been fixed 5 years ago, its stays an argument against f-strings.

It seemed obvious to me that we should favor the use of ".format()" over "%-formatting" [3], at least for newcomers (in the tutorial), as they are expected to run work up-to-date Python interpreters.

It also make it smoother for them to step on (or convert to to) f-strings from the ".format" mini-language than from the "%s" notation. And for those using 3.6+, using f-string is clearly more readable than .format or % (As long as they don't have to do i18n on those f-strings).

The thread in [1] is 5 years old, so I'm starting a new one to see if we can reach a concensus in 2018—2019 about not driving newcomers to use %-formatting.

What do you think?

charettes

unread,
Oct 31, 2018, 1:58:47 PM10/31/18
to Django developers (Contributions to Django itself)
I don't have a strong opinion on whether or not we should make the switch but
if the consensus is too move forward with this I'd suggest we wait until Django
master's branch only supports Python 3.6+.

That would allow us to switch directly to f-strings and avoid doubling the already
large diff noise that such a change would generate.

Simon

Carlton Gibson

unread,
Oct 31, 2018, 3:14:14 PM10/31/18
to Django developers (Contributions to Django itself)
We had a bit of a discussion of this on 


Python 2 docs have got this re format(): 

> This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in new code.


But it's not there for Python 3 (​https://docs.python.org/3/library/stdtypes.html#str.format) so I'm not clear what we're meant to think. 
(I had thought %-formatting deprecated...) 

Anyone know? 

Regardless, waiting a bit and moving straight to f-strings (as Simon suggests) seems sensible. (If we're convinced we do want to change.)

Carlton Gibson

unread,
Oct 31, 2018, 3:20:47 PM10/31/18
to Django developers (Contributions to Django itself)
Sorry, I typed that up and forgot to paste the link 🙄

On Wednesday, 31 October 2018 20:14:14 UTC+1, Carlton Gibson wrote:
We had a bit of a discussion of this on 

Adrian Turjak

unread,
Oct 31, 2018, 5:09:29 PM10/31/18
to django-d...@googlegroups.com
There was a push to deprecated % formatting but too many people complained and that never happened.

While .format and g-strings are superior, % is here to stay for the foreseeable future. Too many people still use it (including myself sometimes).

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/e7cf5b0d-cff4-4c6d-a9ec-1af4a82d5a56%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tom Forbes

unread,
Oct 31, 2018, 6:28:08 PM10/31/18
to django-d...@googlegroups.com
In my experience f strings are vastly more expressive than .format, but not much better than % based formatting.

If we can make this change after we drop 3.5 I think it will be a lot easier.

Tim Graham

unread,
Oct 31, 2018, 6:54:12 PM10/31/18
to Django developers (Contributions to Django itself)
Another discussion is https://groups.google.com/d/topic/django-developers/J9CfWIgrgbY/discussion - % vs {} string formatting for public APIs

I haven't seen any practical benefits to prefer format() over %s. I find the latter less verbose.

I'm not certain what this argument is (things like http://lucumr.pocoo.org/2016/12/29/careful-with-str-format/ I imagine) but here's a previous concern that Florian raised regarding f-strings:
"Knowing what certain members of the core team think about those f-strings, I think there will be first a big discussion if we will allow them at all in Django's codebase. Then there are further things to consider like gettext support etc…"
https://groups.google.com/d/msg/django-developers/4rbVKJYm8DI/y9VNRBw0AgAJ

Ian Foote

unread,
Nov 1, 2018, 8:11:18 AM11/1/18
to django-d...@googlegroups.com
I think {} formatting has some minor readability benefits. The symmetry of the braces makes it easier (imo) to see at a glance what is going to be interpolated. With % formatting the alphabetical characters can be obscured by other text in the string, especially if there's not a space after it. I've often wished string templates in Django form error messages could be overwritten with a {} formatted string. On the other hand making a change would be a lot of churn and I'm not sure the benefit would be worth it.

Ian

charettes

unread,
Nov 1, 2018, 10:37:21 AM11/1/18
to Django developers (Contributions to Django itself)
FWIW it looks like gettext support issues were addressed a while ago[0]

The main issue standing with existing translations is that they'll need to be regenerated[1]
which would require a non-trivial amount of coordination between translation teams I
assume.

Simon

Claude Paroz

unread,
Nov 1, 2018, 1:46:26 PM11/1/18
to Django developers (Contributions to Django itself)
Le jeudi 1 novembre 2018 15:37:21 UTC+1, charettes a écrit :
FWIW it looks like gettext support issues were addressed a while ago[0]

For the .format() syntax, yes. For f-strings, I'm not sure they can be internationalized at all. PEP 498 has not a word about it.

Claude

manda...@gmail.com

unread,
Nov 1, 2018, 5:38:47 PM11/1/18
to Django developers (Contributions to Django itself)
Thanks everybody for your answers.

# Why not wait and jump directly to f-strings?

I like f-strings a lot too, but they does not work with i18n at the moment. (To work with i18n we'll need something like PEP 501 [1], currently deferred).


# Diff would be large :( Translation would be to be rewritten :(

I'm *only* speaking about the examples in the tutorial for the moment, the part of the documentation where newcomers are landing, where they are learning. In this place we have to show them good practices.

So it should be a not-so-big diff, and those examples are not translated.


# What about % being deprecated?

It was a rumor only. It would break too much code, so it's not even being discussed AFAICT.

But from my point of view, it's not because it's not officially deprecated that we should keep teaching new users to use it.





Tim Graham

unread,
Nov 1, 2018, 7:32:29 PM11/1/18
to Django developers (Contributions to Django itself)
I fail to see a problem with %. It's the primary format used throughout Django. The Django tutorial assumes some knowledge of Python, so I'd expect most newcomers would be familiar with both string formatting syntaxes.

Maybe I'm just an old curmudgeon, but I find the tutorial examples more readable and less verbose with % than rewritten with format(). ;-)
Reply all
Reply to author
Forward
0 new messages