Home > Software engineering >  is there a way to delete last comma in each line and print table?
is there a way to delete last comma in each line and print table?

Time:04-18

multiplication table of numbers

hi guys how can print this table(see picture) without last comma(,) in each line? I already tried to move print() or delete (end=) but, I can solve this problem

n=int(input())
for i in range(1, n 1):
  for j in range(1, n 1):
      print(i*j, end=',')
  print()

Output (for input=6):

1,2,3,4,5,6,
2,4,6,8,10,12,
3,6,9,12,15,18,
4,8,12,16,20,24,
5,10,15,20,25,30,
6,12,18,24,30,36,

CodePudding user response:

You can simply place a condition on the value of end, to use a comma when it's not the last element.

for i in range(1, n 1):
  for j in range(1, n 1):
      print(i*j, end=',' if j<n else '')
  print()

CodePudding user response:

I bet you can do this by list comprehension. Take a look at the example:

n = int(input())

for i in range(1, n 1):
    print(*[i*j for j in range(1, n  1)], sep=', ')
  • Related