Luan
Luan

Reputation: 29

Print several values in the same line with commas

I'm new to python (as well as stackoverflow) and I was wondering how I could print several values with commas in between them.

I'm very well aware of the end keyword the print function takes, but the problem is that it appends the string after every value, including the last one, which is precisely what I don't want.

So, instead of maybe 1,2,3,4, ; what I want is 1,2,3,4.

Update: I'm sorry I wasn't so clear as I didn't post my code. Here it is:

N = int(input())
p = []
for i in range(N):
    P = str(input())
    p.append(P)
for i in range(N):
    print(p[N-1-i],end=', ')

N sets the number of following inputs and I want the program to print every entry on the same line but backwards, with each of them separated by comma and space. I think sep doesn't quite work here.

Upvotes: 1

Views: 3216

Answers (2)

Florentin Udrea
Florentin Udrea

Reputation: 15

list1 = ['1','2','3','4']  
s = ",".join(list1) 
print(s)

Upvotes: 0

timgeb
timgeb

Reputation: 78680

print also takes a sep argument which specifies the separator between the other arguments.

>>> print(1, 2, 3, 4, sep=',')
1,2,3,4

If you have an iterable of things to print, you can unpack it with the *args syntax.

>>> stuff_to_print = [1, 2, 3, 4]
>>> print(*stuff_to_print, sep=',')
1,2,3,4

Upvotes: 2

Related Questions