Python如何从print(string,end =“,”)中删除最后一个逗号

时间:2018-09-07 22:57:15

标签: python python-3.x

我从forloop输出的是

string = ""
for x in something:
   #some operation
   string =  x += string 

print(string)

5
66
777

我使用以下代码将它们放在同一行

print(string, end=", ")

然后我得到

5, 66, 777,

我希望最终结果是

 5, 66, 777

如何更改代码print(string, end=", "),以使结尾没有,

上面的字符串是用户输入生成的,它可以是12,3, 45, 98798, 45

到目前为止,我已经尝试过

print(string[:-1], end=", ")                   #result = , 6, 77, 
print((string, end=", ")[:-1])                 #SyntaxError: invalid syntax  
print((string, end=", ").replace(", $", ""))   #SyntaxError: invalid syntax  
print(", ".join([str(x) for x in string]))     # way off result 5
                                                                6, 6
                                                                    7, 7, 7
    print(string, end=", "[:-1])                  #result 5,66,777,(I thought this will work but no change to result)
   print(*string, sep=', ')                          #result 5
                                                             6, 6
                                                             7, 7, 7 

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

user input
twenty five, four, nine 

获取

25, 4, 9, #(that stupid comma in the end)

6 个答案:

答案 0 :(得分:4)

您可以在for循环中构建字符串列表,并使用join打印后缀:

strings = []

for ...:
   # some work to generate string
   strings.append(sting)

print(', '.join(strings))

或者,如果您的something具有明确定义的长度(即您可以len(something)),那么您可以在结束时选择不同的字符串终止符:

for i, x in enumerate(something):
   #some operation to generate string

   if i < len(something) - 1:
      print(string, end=', ')
   else:
      print(string)

基于真实示例代码的更新:

这段代码:

value = input("")
string = ""
for unit_value in value.split(", "):
    if unit_value.split(' ', 1)[0] == "negative":
        neg_value = unit_value.split(' ', 1)[1]
        string = "-" + str(challenge1(neg_value.lower()))
    else:
        string = str(challenge1(unit_value.lower()))

    print(string, end=", ")

根据上面的第一个建议,我得到了:

value = input("")
string = ""
strings = []
for unit_value in value.split(", "):
    if unit_value.split(' ', 1)[0] == "negative":
        neg_value = unit_value.split(' ', 1)[1]
        string = "-" + str(challenge1(neg_value.lower()))
    else:
        string = str(challenge1(unit_value.lower()))

    strings.append(string)

print(', '.join(strings))

答案 1 :(得分:2)

如果您可以先构建一个字符串列表,则可以在print中使用序列解压缩,并使用sep代替end

strings = ['5', '66', '777']

print(*strings, sep=', ')

5, 66, 777

答案 2 :(得分:0)

如果您的for循环还执行除打印之外的其他操作,则可以使用此方法来维护当前结构。

 strings = ['5', '66', '777']

 for i in range(len(strings)-1):
      # some operation

      print(strings[i], end=', ')

 # some operation (last time)
 print(strings[-1])
  
    

5、66、777

  

答案 3 :(得分:0)

l = [5, 66, 777]
print('{0}, {1}, {2}'.format(*l))

答案 4 :(得分:0)

list_1 = [5,10,15,20]

new_list = []
[ new_list.append(f'({i}*{i+5})') for i in list_1 ] 
print(*new_list,sep="+") 

输出

(5*10)+(10*15)+(15*20)+(20*25)

答案 5 :(得分:-1)

for i in range(1,100):
    print (i,end="+")
    
print("100")
<块引用>

#输出=1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+ 23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+ 48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+ 73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+ 98+99+100

最后没有+号