1

I want to print number like "Factors of (input_number) are " 1,2,5,10,50 but is printing with an extra comma like this: 1,2,3,4,5,

input_number = int(input("Enter A Number Please: "))
factors = []
for divisor in range(1, input_number+1):
        mod = input_number % divisor
        if mod == 0:
            factors.append(divisor)
print("Factors of %i are - " % input_number)
for element in factors:
        print(element, end=",")
Silver fang
  • 109
  • 1
  • 3
  • note: the upper limit for your test should be `int(sqrt(input_number)) (+ 1)`. there is no need to go any further. – hiro protagonist Feb 27 '19 at 06:56
  • This [other question](https://stackoverflow.com/questions/438684/how-to-convert-a-list-of-longs-into-a-comma-separated-string-in-python) seems to have your answer. – ferrix Feb 27 '19 at 06:57
  • 2
    Possible duplicate of [How to convert a list of longs into a comma separated string in python](https://stackoverflow.com/questions/438684/how-to-convert-a-list-of-longs-into-a-comma-separated-string-in-python) – ferrix Feb 27 '19 at 06:57
  • 1
    Possible duplicate of [How would you make a comma-separated string from a list of strings?](https://stackoverflow.com/questions/44778/how-would-you-make-a-comma-separated-string-from-a-list-of-strings) – rovyko Feb 27 '19 at 06:58
  • 1
    @rovyko The elements are not strings. – ferrix Feb 27 '19 at 06:59

3 Answers3

2

You can try this:

input_number = int(input("Enter A Number Please: "))
factors = []
for divisor in range(1, input_number+1):
        mod = input_number % divisor
        if mod == 0:
            factors.append(divisor)
print("Factors of %i are - " % input_number+ ",".join(str(row) for row in factors))
Rahil Hastu
  • 558
  • 2
  • 13
2

If you want to print the elements of an iterable with a given separator, you should send that iterable to print and unpack it, and use the sep argument, rather than printing each element separately with the end argument (which, as you saw, puts the given string at the "end" of each element, including the last one).

print(*factors, sep=",")
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I didn't know that the `sep` keyword argument existed until just now. [Link to documentation](https://docs.python.org/3.5/library/functions.html#print) for others. – andrewgu Feb 27 '19 at 07:28
1

In Python, the end keyword argument to print will specify what should be printed after the string you pass in as the first positional argument. So after each element you print, you're telling Python to print a comma (, character) and that's exactly what it is doing.

One way you could do achieve this is by explicitly not setting the end keyword argument for the last item in the array using array enumeration, like so:

print("Factors: ", end="")
for index, element in enumerate(factors):
    if index == len(factors) - 1:
        print(element)
    else:
        print(element, end=",")

Another way you could accomplish this would be to convert your array into strings then use the .join() method, which concatenates the elements of an iterable with a "glue" string.

print("Factors: ", end="")
factors = map(str, factors)
print(",".join(factors))

It may be helpful to note that casting to str should not affect the way non-strings are printed to STDOUT because they would implicitly be cast to strings when printing. For the second method, it may be helpful to reference the Python documentation for map.

andrewgu
  • 1,562
  • 14
  • 23