Is there any operator or symbol, that would allow me to print all the elements in list (separated by comma)?
Because I have this (the list are actually keys from dictionary like dict.keys() ):
mylist = ['string1', 'string2', 'string3']
print mylist[:]
['string1', 'string2', 'string3']
I want the printed to stuff to exclude [
, ]
and '
without using strip
. Is there any way?
PS Sorry to Mods if this is duplicate but I've tried searching for it but came with no results.
print ", ".join([str(x) for x in mylist])
Here's documentation on join.
If mylist
already contains strings, it is enough to do
print ", ".join(mylist)
你可以这样做:
print ", ".join(mylist)
You can do:
import sys
for item in mylist:
sys.stdout.write(item+", ")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.