Hi All,
I am writing a program in which I take data for a table from the user, and then ask for the alignment for each column. Then I have to display it. Each column mush be separated by a tab and a space.
For example, let's say the user inputs that they want a 3 x 3 table to display "help I don't know how to do these tables."

 Help      I       dont
 know     how        to
 do      these   tables

Thanks!

Use the String.Formate method like
String.Format("{0,10:D}", 2)
This will format number 2 as a string of width 10 aligned to the right, use -10 to have it aligned on the left

There are two ways to do this, though one of them only works on 2.5 and later (it was developed for Python 3, then later backported to Python 2). In either approaches, you first need to use split() to divide the string into a list of individual words.

aprime = "help I don't know how to do these tables.".split(' ')

The first way to do this is to us string interpolation syntax, which provides a minimum field width argument to the formatting specifier.

print("%s%10s%10s\n%s%10s%10s\n%s%10s%10s" % tuple(aprime))

The second approach, which is a bit more flexible but is only supported under newer versions of the language, is to use the string method format(). This also has field width sub-field in its formatting specifiers, but can also define whether the word should be to the right, to the left, or to the center:

print("{0:<10}{1:^10}{2:>10}\n{3:<10}{4:^10}{5:>10}\n{6:<10}{7:^10}{8:>10}".format(*aprime))

You first split the sentence into words, then print accordingly. I will leave dealing with punctuation to you. A simplified example:

test_input="help I don't know how to do these tables."
test_list=test_input.split()
ctr=0
for outer in range(3):
    for inner in range(3):
        print test_list[ctr],
        ctr += 1
    print
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.