Definite's Extractor

My findings on Life, Linux, Open Source, and so on.

Legacy Python str.format() gotcha

In Python 2.7, str.format() works with empty string like:

python -c "print '{}'.format('')"

However, in Python < 2.6,  you will see:

ValueError: zero length field name in format

Workaround? Use %-formatting like:

python -c "print '%s' % ''"

 

4 responses to “Legacy Python str.format() gotcha

  1. Ioannis Liverezas 2018/09/03 at 5:56 pm

    Small typo: You have three single quotes instead of two in the python 2.7 version.

    Like

  2. Adam Williamson 2018/09/05 at 10:06 am

    It’s easier just to do this:

    python -c “print ‘{0}’.format(”’)”

    Like

Leave a comment