Skip to content Skip to sidebar Skip to footer

How To Comma Separate An Array Of Integers In Python?

I have a list of integers, i want to convert them into comma separated integers, how can i actually achieve that The result should have comma separated integers, not comma separate

Solution 1:

You can convert them to strings and then use str.join:

integers = [106, 386, 53]

', '.join(map(str, integers))

Output:

'106, 386, 53'

Solution 2:

Assuming that you have list of numbers:

numbers = [220.0, 112.9]

and you just want to print:

220.0, 112.9

you could do:

print(*numbers,sep=', ') # prints 220.0, 112.9

Star-operator (also known as unpack operator) means that element of list which follows will be used as subsequent arguments. sep=', ' means that printed element will be separated by , followed by space.

Post a Comment for "How To Comma Separate An Array Of Integers In Python?"