1

I have the following code:

for n in range(1, 101):
    if 100 % n == 0:
        print(n, end='')

I want to print all divisors of 100. However, I want them in one line (which I accomplish by putting end='' in the code). On top of that, I want commas between the numbers. I want to have an output like this:

1, 2, 4, 5, 10, 20, 25, 50, 100

sep=',' does not work because of the loop it is in. end=',' will work, but this will lead to a comma after 100, which is not what I want.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Veerle
  • 43
  • 4

6 Answers6

2

You can create a list and then use str.join. For example:

lst = []
for n in range(1, 101):
    if 100 % n == 0:
        lst.append(str(n))
print(','.join(lst))

Example/Demo -

>>> lst = []
>>> for n in range(1, 101):
...     if 100 % n == 0:
...         lst.append(str(n))
...
>>>
>>> print(','.join(lst))
1,2,4,5,10,20,25,50,100

This can also be done in single line, by using a list comprehension:

print(','.join([str(n) for n in range(1, 101) if 100 % n == 0]))

Demo:

>>> print(','.join([str(n) for n in range(1, 101) if 100 % n == 0]))
1,2,4,5,10,20,25,50,100
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1

You need to store values into a list and then use .join(). Moreover, note that you have to cast elements in your list to string.

my_list = []
for n in range(1, 101):
    if 100 % n == 0:
        my_list.append(n)

print(", ".join(str(x) for x in my_list))
Delgan
  • 18,571
  • 11
  • 90
  • 141
1

I don't have enough reputation to add a comment to Anand S Kumar, but I want to point out that you don't need a list inside join since it can take a generator expression. So you can also do:

>>> print(", ".join(str(n) for n in range(1, 101) if 100 % n == 0))
1, 2, 4, 5, 10, 20, 25, 50, 100

edit: I had a comment stating that this was faster. It turns out that this was incorrect.

Jean Nassar
  • 555
  • 1
  • 5
  • 13
0

Try this

ls = []
for n in range(1, 101):
if 100 % n == 0:
    ls.append(str(n))

print (','.join(ls))
San k
  • 141
  • 9
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. It's also recommended that you don't post an answer if it's just a guess. A good answer will have a plausible reason for why it could solve the OP's issue. – SuperBiasedMan Sep 08 '15 at 11:27
  • Please fix indentation and explain what you are doing. – Michele d'Amico Sep 08 '15 at 11:56
0

you can use string formatting for that

for n in range(1, 101):
if 100 % n == 0:
    print "%s," %n,

output

1, 2, 4, 5, 10, 20, 25, 50, 100,
0

Try this:

mylist = []
mynumber = 100
for n in range(1,(int(mynumber/2))+1):
    if mynumber % n == 0:
        mylist.append(str(n))
mylist.append(mynumber)
print (','.join([str(item) for item in mylist]))

You don't need to try everything up to the number you are testing because it wont have a factor bigger than its half. E.g. 10 doesn't have a factor after 5 except 10 itself.

You can change "mynumber" to whichever number you wish to test.

Farhan.K
  • 3,425
  • 2
  • 15
  • 26