my_list = ['a', 'b', 'c', 'd']
my_string = ','.join(my_list)
'a,b,c,d'
This won't work if the list contains integers
And if the list contains non-string types (such as integers, floats, bools, None) then do:
my_string = ','.join(map(str, my_list))
Answer from Mark Biek on Stack Overflowmy_list = ['a', 'b', 'c', 'd']
my_string = ','.join(my_list)
'a,b,c,d'
This won't work if the list contains integers
And if the list contains non-string types (such as integers, floats, bools, None) then do:
my_string = ','.join(map(str, my_list))
Why the map/lambda magic? Doesn't this work?
>>> foo = ['a', 'b', 'c']
>>> print(','.join(foo))
a,b,c
>>> print(','.join([]))
>>> print(','.join(['a']))
a
In case if there are numbers in the list, you could use list comprehension:
>>> ','.join([str(x) for x in foo])
or a generator expression:
>>> ','.join(str(x) for x in foo)
python - How to convert a list to a comma separated string? - Stack Overflow
Converting word values to comma separated list with ‘, and ‘ before last value.
How to convert a list of longs into a comma separated string in python - Stack Overflow
Convert a comma separated property to a List
Videos
Depends on your result and description, I guess your code is something like:
b = """58738121385139B
9135A4251EF0
2B631B53C383"""
print(', '.join(b))
result:
5, 8, 7, 3, 8, 1, 2, 1, 3, 8, 5, 1, 3, 9, B,
, 9, 1, 3, 5, A, 4, 2, 5, 1, E, F, 0,
, 2, B, 6, 3, 1, B, 5, 3, C, 3, 8, 3
You should use split to achieve it
print(', '.join(b.split("\n")))
result:
58738121385139B, 9135A4251EF0, 2B631B53C383
Seems like your string consists of space-separated chunks of data, You could try this:
a = b.replace(" ", ",")
I’m new to Python, and, unfortunately, have submitted myself to an operational path of a cybersecurity masters program. This weeks assignment had us take a list (apples, bananas, tofu, cats) and modify the the function to print out: ‘apples, bananas, tofu, and cats’ The script that they gave us to modify was the following:
if name == ‘main’: spam = [‘apples’, ‘bananas’, ‘tofu’, ‘cats’] spam_str = ‘’ for spam_item in spam: print(“Spam item: “ + spam_item) spam_str = spam_str + spam_item print(“Spam str: “ + spam_str) print(spam_str)
As written, it ends up with the output:
Spam item: apples Spam str: apples Spam item: bananas Spam str: applesbananas …. Spam str: applesbananastofucats
Somehow, in line 6, we were supposed to modify the function only output the desired:
apples, bananas, tofu, and cats
I’ve been banging my head on my desk trying to figure this out and have gotten as far as modifying the for statement to get me:
apples, bananas, tofu, cats,
For the life of me, I can’t seem to find the correct way to get just the 3 commas and an ‘and’ before the last item.
To make it more interesting, we were given other lists as well in which we should be able to input the same modified solution to add those commas and the final ‘, and ‘ (other samples were names of cities and other items with varying lengths of items listed).
I hoping the power of Reddit can figure out an answer while explaining how it works. I’m the type of person that needs to see the answer to figure out the mechanics behind it and be able to apply it to different situations….