I believe this satisfies the requirements: >>> import os, sys, string >>> def f ( s ): ... return "%-5.5s" % s ... >>> f('doug') 'doug ' >>> f('douglas') 'dougl' >>>