[Tutor] tabbed output

Modulok modulok at gmail.com
Mon Feb 13 03:20:17 CET 2012


On 2/12/12, Michael Lewis <mjolewis at gmail.com> wrote:
> I am having a weird issue. I have a print statement that will give me
> multiple outputs separated by a tab; however, sometimes there is a tab
> between the output and sometimes there is not. It seems sort of sporadic.
> My code is below and two sample outputs are below that (one that works how
> I expect, and the other showing the issue (tab separation not occurring
> between all pieces of the output) What is going on?
>
> def CheckNames():
>     names = []
>     for loop in range(1,4):
>         while True:
>             name = raw_input('''Name #%s: ''' %(loop))
>             if name not in names:
>                 names.append(name)
>                 break
>             print '%s is already in the data. Try again.' %(name)
>     sorted_names = sorted(names)
>     for element in list(sorted_names):
>         print 'Hurray for %s!\t' %(element),
>
> Sample Output1:
>
> Name #1: mike
> Name #2: bret
> Name #3: adam
> Hurray for adam!            Hurray for bret!   Hurray for mike!
>
> Sample Output2(there is a tab between output 2 & 3, but not 1 & 2):
>
> Name #1: abe
> Name #2: alan
> Name #3: adam
> Hurray for abe! Hurray for adam!              Hurray for alan!
>

You should use spaces, not tabs. Tabs only align with tab stops, which will
depend on the length of the words (names). To make it easy to use spaces
instead, use the 'format()' method available on string objects. A one-line
modification of your could would look like this::

    def CheckNames():
        names = []
        for loop in range(1,4):
            while True:
                name = raw_input('''Name #%s: ''' %(loop))
                if name not in names:
                    names.append(name)
                    break
                print '%s is already in the data. Try again.' %(name)
        sorted_names = sorted(names)
        for element in list(sorted_names):
            print "{value:<30}".format(value="Hurray for %s!" % element),
                # The line above is all that was changed.


The result, is that 'value' will be output as left-aligned '<', and a minimum
of 30 characters wide '30'. The 'value' is specified as a keyword argument to
the format() method. In the example above, 'value' is also making use of
python's older string formatting method. Using a monospaced font, your output
will always be lined up, as long as the 'value' string never exceeds 30
characters wide.

You can optionally *not* specify the 'value' variable and instead use a
positional argument to the format method like this, but it makes it less clear
what you're doing::

    print "{0:<30}".format("Hurray for %s!" % element),

As a final note, if 'CheckNames' is a function and not a method, it should be
all lowercase, or use_underscores rather than camelCase. This is not enforced
by python, but is kind of the de-facto standard.

Read more about the format method here:

http://docs.python.org/library/string.html#formatspec
http://docs.python.org/library/string.html#formatstrings


-Modulok-


More information about the Tutor mailing list