[Biopython-dev] Using .format() method of strings in Biopython?

Tiago Antao tra at popgen.net
Thu Nov 5 03:40:31 UTC 2015

On Tue, 13 Oct 2015 12:03:03 +0100
Peter Cock <p.j.a.cock at googlemail.com> wrote:

> As part of https://github.com/biopython/biopython/pull/635
> Carlos is suggesting replacing some of the % string formatting
> (which is like the C sprintf function) with the Python
> string .format() method.
> e.g.
> "Codon '%s' is invalid" % codon
> becomes:
> "Codon '{0}' is invalid".format(codon)
> https://pyformat.info/
> https://docs.python.org/2/library/string.html#formatstrings
> https://docs.python.org/2/library/stdtypes.html#string-formatting
> Note the later says the following about the .format method:
> "This method of string formatting is the new standard in Python 3"

While I am a certified Python 3 zealot, in this case I do not think
this is always a great idea.

string % values is fine for simple cases (like the above, for example).
It is problematic in cases with tuples and dictionaries, but not in
very simple stuff that we tend to use a lot - and that is fine.

While the format method is the new standard, % is in now way going to
be deprecated - We can keep it without fear of breaking future
compatibility or existing Python 3 features.

I would maintain neutrality in this case.

More information about the Biopython-dev mailing list