1

How do I put full stops (.) at the end of each line in the code below?

num = 0

for i in range(0, 3):     
  for j in range(0, 5):
    print(num, end=",")
    num = num + 1
     
  print("\r")

Current output:

0,1,2,3,4, 
5,6,7,8,9, 
10,11,12,13,14,

Output I want:

0,1,2,3,4.
5,6,7,8,9.
10,11,12,13,14.

Thank you in advance for any help!

Thi Nguyen
  • 33
  • 4
  • 1
    An easier solution might be to generate your lists and then use `join` to output a comma-separated string, and then `print('.')` at the end. https://stackoverflow.com/questions/497765/python-string-joinlist-on-object-array-rather-than-string-array – WOUNDEDStevenJones Mar 31 '21 at 16:49

4 Answers4

0

num = 0

for i in range(0, 3):     
for j in range(0, 5):
    print(num, end=", ")
    print('.')
    num = num + 1
     
print("\r")

This should work. As the end of your variable 'num' when printed has been set to a comma and a space, printing the full stop should work in this way

SamTheProgrammer
  • 1,051
  • 1
  • 10
  • 28
0
num = 0

for i in range(0, 3):     
  for j in range(0, 5):
    if j == 4:
        print(num, end=". ")
    else:
        print(num, end=", ")
    num = num + 1
     
  print("\r")
luanpo1234
  • 300
  • 1
  • 7
  • Thank you so much :) this works well for me – Thi Nguyen Mar 31 '21 at 16:56
  • 1
    @ngocnguyen Glad I could help. I will say this only because you're new and might not know the functionality: if you're satisfied with my answer, you could "accept" it by using the corresponding StackOverflow function. Which is of course totally voluntary. – luanpo1234 Mar 31 '21 at 17:07
0

You can do such like this. Hope this will work on your side

i_range = 3
j_range = 5
num = 0

for i in range(0, i_range):
    for j in range(0, j_range):
        ends_ = '.' if j_range - j == 1 else ','
        print(num , end=ends_)
        num +=1 
    print('\r')
ZeevhY Org.
  • 295
  • 3
  • 8
0

You can build a dynamic list with the second range, then print it using *. This allows you to put the , character as separator, and also the . character as end in the same line.

num = 0
step = 5
for i in range(0, 3):
    print(*range(num, num + step), sep=", ", end=". ")
    num = num + step

    print("\r")
cr_alberto
  • 330
  • 1
  • 7
  • 17