简体   繁体   中英

Python3 list printing without spaces

i hvae this code here. i want to print a list without spaces. here l is a list with 3 elements that i trying to print:

>>> l=[]
>>> l.append(5)
>>> l.append(6)
>>> l.append(7)
>>> print(l)

i get in the output:

[5, 6, 7]

but i wanna get:

[5,6,7]

what should i add to the syntax in append or in print to print the list without spaces

您需要使用类似:

print('[{0}]'.format(','.join(map(str, l))))

如果结果太大,可以修改:

print(repr(l).replace(' ', ''))

加入列表元素。

print("[" + ",".join([str(i) for i in l]) + "]")

You could convert it to a string and then replace the spaces. Eg:

print ("{}".format(l)).replace(' ', '')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM