2

Right now I'm using print(), calling the variables I want that are stored in a tuple and then formatting them using: print(format(x,"<10s")+ format(y,"<40s")...) but this gives me output that isn't aligned in a column form. How do I make it so that each row's element is aligned?

So, my code is for storing student details. First, it takes a string and returns a tuple, with constituent parts like: (name,surname,student ID, year).
It reads these details from a long text file on student details, and then it parses them through a tuplelayout function (the bit which will format the tuple) and is meant to tabulate the results.
So, the argument for the tuplelayout function is a tuple, of the form:
surname | name | reg number | course | year

0

2 Answers 2

6

If you are unpacking tuples just use a single str.format and justify the output as required using format-specification-mini-language:

l = [(10,1000),(200,20000)]

for x,y in l:
    print("{:<3} {:<6}".format(x,y))

10  1000  
200 20000 
Sign up to request clarification or add additional context in comments.

5 Comments

This can't fit into my code, because you're using a list of tuples. If you try it on just one tuple, then you get an error: "ValueError: need more than 1 value to unpack", and I don't know how to fix that. Any help?
well you cannot have x and y variables if you only have a single element, you can check if your tuple has one or two items, what are x and y supposed to be in your own code?
My code also works on a single tuple but I don't see how a single tuple will form a table or why would need to justify single elements
So, my code is for storing student details. First, it takes a string and returns a tuple, with constituent parts like: (name,surname,student ID, year). It reads these details from a long text file on student details, and then it parses them through a tuplelayout function (the bit which will format the tuple) and is meant to tabulate the results. So, the argument for the tuplelayout function is a tuple, of the form: surname | name | reg number | course | year Can I simply
Can I simply use print("{:<10} {:<22}{:<7}{:<5}{:<5}".format(tple))?
-1

My shell has the font settings changed so the alignment was off. Back to font: "Courier" and everything is working fine.
Sorry.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.