9

I just learned you can write

'{}{}'.format(string_a, string_b)

instead of

'{0}{1}'.format(string_a, string_b)

in Python, i.e. you can omit the numerals for the string format parameters when you want things to slot in one by one in order.

Is this considered Pythonic?

NOTE: "Pythonic" is a commonly used term among Python programmers to mean idiomatic Python code. Within Python culture, there tends to be clear consensus on style questions, especially for very specific ones like this one, given the language's explicit design philosophy of "There should be one -- and preferably only one -- obvious way to do it." This is quoted from "The Zen of Python," a set of aphorisms which goes a long way towards defining what is "Pythonic" and which is included with every distribution of Python (at any Python interpreter command line, enter import this to see it).

  • 4
    @GregHewgill: "Pythonic" is a pretty commonly used term that basically means "idiomatic Python"; the sort of Python code that aficionados of quality Python code would find stylistically pleasing. – Carson63000 Feb 12 '13 at 3:04
  • 1
    This very specific feature is clearly included in Python (it's not really a style question), so why wouldn't it be considered "Pythonic" by any reasonable definition? As far as I know, there are no features of the language that are considered "unPythonic". – Greg Hewgill Feb 12 '13 at 3:08
  • 4
    For reference, the {} feature was introduced in Python 3.1. The enhancement that prompted this addition was issue 5237. Guido has several comments on that issue, including "Please go ahead and finish this. I'm glad this is going in!" If that's not considered "Pythonic", I don't know what is. – Greg Hewgill Feb 12 '13 at 3:14
  • 4
    To folks who are voting to close this question, may I just point out that what is or is not Pythonic is an important part of Python coding culture, and I'd argue coding culture is an important element of software engineering, an accepted subject of this site. – Ghopper21 Feb 12 '13 at 4:23
  • 2
    @Andrea - that's exactly what my first thought was, but it's hard to argue that the BDFL thinks it's un-Pythonic. After seeing the quote of him on this, I went back at looked at the Zen of Python again, and thought maybe this is a case of "practicality beats purity"? The rationale in the discussion (linked in Greg's answer) is around convenience for the code writer. And in any case, really the most explicit way to use string formatting is "{a}{b}".format(a=string_a, b=string_b) rather than the numbering. Anyhow, that's just my speculation and reading between the lines on the thinking... – Ghopper21 Feb 12 '13 at 12:11
12

The {} feature was introduced in Python 3.1 (and also backported to Python 2.7). The enhancement that prompted this addition was issue 5237. Guido has several comments on that issue, including:

Please go ahead and finish this. I'm glad this is going in!

A feature with that sort of endorsement from the BDFL would certainly be considered "Pythonic" by any measure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.