Python string formatting in print statements

#!/usr/bin/env python

# Format using a dictionary
print '%(language)s has %(number)03d quote types.'
% {"language":"Python","number":2}

# Format using a list

print '%s has %03d quote types.' % ("Python",2)

# Best method

print '{} has {} quote types.'.format("Python",2)

Planning to post more code snippets this year to remind me of how to do various things in Python.

Source: Pyformat

Comments

Popular posts from this blog

Database, schema, and table sizes in Greenplum

Greenplum update with multiple tables

Show running queries on Postgresql/Greenplum