0

I have a class in python with 2 "main" strings and a list of strings in it with N size

How can I "nicely" format the print of it so i can print the pain list and it will look normal.

The expected result is like:

entry 1 : string1 string 2 <br>
liststring1 liststring2 .... liststring n

entry 2 : string1 string 2 <br>
liststring1 liststring2 .... liststring n

.<br>
.<br>
.<br>
entry n : string1 string 2 <br>
liststring1 liststring2 .... liststring n

3 Answers 3

1

Let's presume your python class, Entry, has the following member variables:

Entry.string1  
Entry.string2  
Entry.list_of_strings  

Then to print a list_of_entries:

>>> for entry in list_of_entries:
...     print entry, " : ", entry.string1, entry.string2
...     for s in entry:
...         print s,
...     print ""
Sign up to request clarification or add additional context in comments.

Comments

0
>>> l
[[1, 2, 3], [1, 2, 3]]
>>> for entry in l:
...    print entry, ":", 
...    for x in entry:
...        print x,
...    print

Notice the trailing commas in print statements.

Comments

0

Try the pprint module for easy printing of 2D lists, among other things.

import pprint
x=[[1,2,3,4,5,6,7,8,9,10,11],['jkdfjkdfjkfdjkfdkjfdkdfjfkdjdkfjkdjkfd']]
pprint.pprint(x)

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.