Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 我想从列表末尾删除最后一个结束符号_String_List_Python 3.x - Fatal编程技术网

String 我想从列表末尾删除最后一个结束符号

String 我想从列表末尾删除最后一个结束符号,string,list,python-3.x,String,List,Python 3.x,我想删除3之后的最后一个:-) 我可以想出三种合理的方法来做到这一点 第一种方法是显式地检查您正在迭代的值,以决定要传递给打印的结束。由于您直接在范围内进行迭代,因此非常简单,只需测试最后一个值: >>> for i in range(4): ... print(i, end=" :-) ") ... 0 :-) 1 :-) 2 :-) 3 :-) >>> 最后一个选项在Python3中可用(在Python2的更高版本中,如果您使用from\uuuu

我想删除3之后的最后一个:-)

我可以想出三种合理的方法来做到这一点

第一种方法是显式地检查您正在迭代的值,以决定要传递给
打印
结束
。由于您直接在
范围内进行迭代
,因此非常简单,只需测试最后一个值:

>>> for i in range(4):
...     print(i, end=" :-) ")
...
0 :-) 1 :-) 2 :-) 3 :-) >>>
最后一个选项在Python3中可用(在Python2的更高版本中,如果您使用
from\uuuuuu future\uuuuuu import print\u函数
获取Python3版本的
print
)。使用参数解包将整个
数字范围
一次传递给
打印
函数,并将笑脸作为
sep
参数传递,而不是
结束

print(" :-) ".join(str(i) for i in range(4)))
这可能是最好的解决方案。启用这种代码是
print
成为Python3函数的主要原因之一(不能将序列解压为Python2
print
语句的参数)

print(" :-) ".join(str(i) for i in range(4)))
print(*range(4), sep=" :-) ")