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).

9
  • 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. Commented Feb 12, 2013 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". Commented Feb 12, 2013 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. Commented Feb 12, 2013 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
    Commented Feb 12, 2013 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
    Commented Feb 12, 2013 at 12:11

1 Answer 1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.