2017-04-17 90 views
0

当进入下面的代码我得到完全的输出我想:打印列表标记

entrants = ['a','b','c','d'] 
# print my list with square brackets and quotation marks 
print (entrants) 

#print my list without brackets or quotes 
#but all on the same line, separated by commas 
print(*entrants, sep=", ") 

#print my list without brackets or quotes, each element on a different line 
#* is known as an 'identifier' 
print(*entrants, sep="\n") 

然而,当我输入以下代码:

values = input("Input some comma separated numbers: ") 
List = values.split(",") 
Tuple = tuple(List) 
print('List : ', List ) 
print('Tuple : ', Tuple) 
print('List : ', sep=",", *List ) 
print('Tuple : ', sep=",", *Tuple) 

我得到一个空间和逗号前的最后两行输出的第一个值如下:

List : ['1', '2', '3'] 
Tuple : ('1', '2', '3') 
List : ,1,2,3 
Tuple : ,1,2,3 

我该怎么做错了吗?

+1

你不认为它会算初始的字符串? –

回答

0

使用“SEP的“in print在另外两个参数之间插入分隔符。

>>> print("a","b",sep="") 
ab 

试试这个:

>>> def print_sameline(): 
...  list = [1,2,3] 
...  print("List: ",end="") 
...  print(*list,sep=",") 
... 
>>> print_sameline() 
List: 1,2,3 
0

使用sep使分离去所有的参数之间包括'List : ''Tuple : ',所以你应该使用.join(),而不是加入列表/元组与","作为分隔符:

print('List : ', ",".join(List)) 
print('Tuple : ', ",".join(Tuple)) 
+0

谢谢,它工作:) – sleepylog