list = [1, 2, 3, 4]
for element in list:
print(element, end=" ")
For this code, the output is: 1 2 3 4 .
But there is a space after the last element(4).How can I remove only that last space?I tried adding sep=" " before the end=" " but it doesn’t work.
>Solution :
The easiest solution would be to print everything, but the last element with space, then print the last without a space:
lst = [1, 2, 3, 4]
for i in range(len(lst) - 1):
print(lst[i], end=" ")
print(lst[len(lst) - 1])