EDIT 2013-12-11 - This answer is very old. This is still true and correct, but people looking at it should prefer the new format syntax .
You can use string formatting as follows:
>>> print '%5s' % 'aa' aa >>> print '%5s' % 'aaa' aaa >>> print '%5s' % 'aaaa' aaaa >>> print '%5s' % 'aaaaa' aaaaa
Basically:
- the
% character tells Python that it should replace something with a token - the
s character tells python that the token will be a string 5 (or any other desired number) informs Python of the need to pad the string with spaces of up to 5 characters.
In your particular case, a possible implementation might look like this:
>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1} >>> for item in dict_.items(): ... print 'value %3s - num of occurances = %d' % item
Side note: just wondering if you know about the existence of the itertools module . For example, you can get a list of all your combinations on one line with:
>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)] ['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
and you can get the number of occurrences using combinations in combination with count() .