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.


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

    It’s easier just to do this:

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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: