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']]))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.