Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
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
2  
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
1  
As the Zen of Python goes, "Explicit is better than implicit". So in my view this does not look very pythonic –  Andrea Feb 12 '13 at 10:08

1 Answer 1

up vote 10 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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