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
.