How to print a string at a fixed width?
I find using str.format
much more elegant:
>>> '{0: <5}'.format('s')'s '>>> '{0: <5}'.format('ss')'ss '>>> '{0: <5}'.format('sss')'sss '>>> '{0: <5}'.format('ssss')'ssss '>>> '{0: <5}'.format('sssss')'sssss'
If you want to align the string to the right use >
instead of <
:
>>> '{0: >5}'.format('ss')' ss'
Edit 1:As mentioned in the comments: the 0
in '{0: <5}'
indicates the argument’s index passed to str.format()
.
Edit 2:In python3 one could use also f-strings:
sub_str='s'for i in range(1,6): s = sub_str*i print(f'{s:>5}') ' s'' ss'' sss'' ssss''sssss'
or:
for i in range(1,5): s = sub_str*i print(f'{s:<5}')'s ''ss ''sss ''ssss ''sssss'
of note, in some places above, ' '
(single quotation marks) were added to emphasize the width of the printed strings.
EDIT 2013-12-11 - This answer is very old. It is still valid and correct, but people looking at this should prefer the new format syntax.
You can use string formatting like this:
>>> print '%5s' % 'aa' aa>>> print '%5s' % 'aaa' aaa>>> print '%5s' % 'aaaa' aaaa>>> print '%5s' % 'aaaaa'aaaaa
Basically:
- the
%
character informs python it will have to substitute something to a token - the
s
character informs python the token will be a string - the
5
(or whatever number you wish) informs python to pad the string with spaces up to 5 characters.
In your specific case a possible implementation could look like:
>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}>>> for item in dict_.items():... print 'value %3s - num of occurances = %d' % item # %d is the token of integers... value a - num of occurances = 1value ab - num of occurances = 1value abc - num of occurances = 1
SIDE NOTE: Just wondered if you are aware of the existence of the itertools
module. For example you could obtain a list of all your combinations in 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 could get the number of occurrences by using combinations
in conjunction with count()
.
Originally posted as an edit to @0x90's answer, but it got rejected for deviating from the post's original intent and recommended to post as a comment or answer, so I'm including the short write-up here.
In addition to the answer from @0x90, the syntax can be made more flexible, by using a variable for the width (as per @user2763554's comment):
width=10'{0: <{width}}'.format('sss', width=width)
Further, you can make this expression briefer, by only using numbers and relying on the order of the arguments passed to format
:
width=10'{0: <{1}}'.format('sss', width)
Or even leave out all numbers for maximal, potentially non-pythonically implicit, compactness:
width=10'{: <{}}'.format('sss', width)
Update 2017-05-26
With the introduction of formatted string literals ("f-strings" for short) in Python 3.6, it is now possible to access previously defined variables with a briefer syntax:
>>> name = "Fred">>> f"He said his name is {name}."'He said his name is Fred.'
This also applies to string formatting
>>> width=10>>> string = 'sss'>>> f'{string: <{width}}''sss '