119

I'm trying learn Python (3 to be more specific) and I'm getting this error:

ValueError: zero length field name in format

I googled it and I found out you need to specify the numbers:

a, b = 0, 1
if a < b:
     print('a ({0}) is less than b ({1})'.format(a, b))
else:
     print('a ({0}) is not less than b ({1})'.format(a, b))

And not like the tutorial (from lynda.com) actually says to do:

a, b = 0, 1
if a < b:
     print('a ({}) is less than b ({})'.format(a, b))
else:
     print('a ({}) is not less than b ({})'.format(a, b))

The tutorial im following has Python 3.1, and im using 3.2 and what i read about this error is that this only happens in <3.1 (3.0). Did they undo this in 3.2, or am i doing something wrong?

Also, speak slowly ;) this is literally my first night learning Python and only the 2nd "script" i've written in Python.

Oscar Godson
  • 31,662
  • 41
  • 121
  • 201
  • 1
    Try adding this: import sys; print(sys.version), just to make sure that you aren't accidentally calling up some other version of python – Winston Ewert Mar 27 '11 at 04:00

3 Answers3

148

Python 2.6 and 3.0 require the field numbers. In Python 2.7 and later and 3.1 and later, they can be omitted.

Changed in version 2.7: The positional argument specifiers can be omitted, so '{} {}' is equivalent to '{0} {1}'.

python2.6.4>>> print '|{0:^12}|{1:^12}|'.format(3,4)
|     3      |     4     |
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • 2
    The version difference jacked me up today! Thank for the pointer. – Richard Clayton Nov 25 '13 at 20:32
  • @Dennis: I voted for your answer because you showed how to address the problem in earlier versions of python, by specifying the field number. However, saying "Python 2.7 and later" seems misleading to me because according to other answers here, the field numbers are required in Python 3.0. Winston points out in the last comment on his answer that 2.7 came out after 3.0, so your statement may be chronologically correct, but most of us would read it as indicating that empty braces should work in 3.0. – Tom Barron Oct 22 '15 at 20:31
  • @TomBarron: Empty braces work for me: `python3.4 -c 'print("|{}|{}|".format(3,4))'`, but apparently 3.0 doesn't have it (3.1 and later does). I'll clarify my answer. – Dennis Williamson Oct 23 '15 at 16:29
130

I'm gonna guess that you are running python 2.6 by accident somehow.

This feature is only available for at least 3.1 if you are using python 3, or 2.7 if you are using python 2.

Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
  • 3
    Thanks! that was it! Had to change my shebangs to: `#! /usr/local/bin/python3` after looking all over. There is 3 different locations of python installs on Mac -_- wtf. – Oscar Godson Mar 27 '11 at 04:16
  • 4
    This is partially correct: OP's code would work if it was python 2.7, but not 2.6. (See @Dennis Williamson's answer). – mayhewsw Jan 16 '14 at 18:04
  • I'm getting this problem with Python 3.0.1 on Windows. ( It definitely says 3.0.1 in the first line after starting Python cmdline) – inger Apr 26 '15 at 22:00
  • @inger, you need python 3.1 to use it. – Winston Ewert Apr 26 '15 at 22:01
  • hmm, thanks for the quick reply..however, the answers below says "2.7 and later" - so 3.0.1 was earlier? Also, the OP asked about the problem in 3.0 (+ others) - to which your answer is "you are running 2.6", which would suggest 3.0 itself is ok. It would be nice if you could update the answer. Thanks – inger Apr 26 '15 at 22:10
  • @inger, the question states that its not available in 3.0. I can see how it would be confusing, but 2.7 did come out after 3.0. – Winston Ewert Apr 26 '15 at 22:11
2

If you're using Eclipse you should look into Window -> Preferences -> PyDev -> Interpreter - Python. There you have a list of interpreters (with name and location). If for your current project you're using interpreter which is located for example in /usr/bin/python then probably executing /usr/bin/python -V whill give you something like "Python 2.6.6". And there is your answer like Winston Ewert wrote.

(you can add new interperter by simply clicking "New..." button and giving /usr/bin/python3 as "location". Then you have probably to change your project settings (Preferences -> PyDev - Interpreter/Grammar).

ilektrik
  • 511
  • 4
  • 3