0
numbers = []
for i in range(2000, 3201):
   if (i % 7) == 0 and (i % 5) !=0:
       numbers.append(i)

numbers = ' '.join([str(elem) for elem in numbers])

print(numbers)

When printed, it doesn't have commas between the numbers. How can I do that?

2 Answers2

2

You can just leave the numbers list as is, without joining anything, and directly unpack the list inside of print() invocation:

print(*numbers, sep=",")
Max Shouman
  • 1,333
  • 1
  • 4
  • 11
0

Use this:

numbers = []
for i in range(2000, 3201):
   if (i % 7) == 0 and (i % 5) !=0:
       numbers.append(i)

numbers = ','.join([str(elem) for elem in numbers])

print(numbers)
j__carlson
  • 1,346
  • 3
  • 12
  • 20
  • I understand that there are better methods, this is just the closest solution to the questions askers attempted method. If you have a different method you should list it with the other answers to give the question asker a better scope of the different ways to solve the problem. – j__carlson Jul 27 '21 at 00:39
  • 1
    @DemianWolf Actually, it is better to pass `list` instead of generator because `str.join` would convert it into list anyway. Take a look at https://stackoverflow.com/questions/37782066/list-vs-generator-comprehension-speed-with-join-function – Chris Jul 27 '21 at 02:09