Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function def show_table(table): Given table as a list of lists of string

ID: 3679590 • Letter: W

Question

Write a function def show_table(table): Given table as a list of lists of strings, create and return a formatted string representing the 2D table. Follow these requirements (examples below):

o Each row of the 2D table is on a separate line; the last row is followed by an empty line o Each column is aligned to the left;
o Columns are separated with a single vertical bar '|'; and there must be exactly one spacebefore and after the vertical bar;

o Every row starts and ends with a vertical bar; and there must be exactly one space after theleading bar and before the ending bar

o Restriction: you must use string formatting, either % or .format().
o Hint: Find out the max width for every column before you start creating the string.
o Assumption: every row of the 2D table has the same number of columns.

Example:

print(show_table([['A','BB'],['C','DD']]))

| A | BB |
| C | DD |

Explanation / Answer

def show_table(table):
string = ''
for i in table:
for j in range(len(i)):
line = i[j]
string += '| {} '.format(i[j])
string += '| '
return string
print(show_table([['A','BB'],['C','DD']]))   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote