Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
Python 字符串中的列表和打印格式_Python - Fatal编程技术网

Python 字符串中的列表和打印格式

Python 字符串中的列表和打印格式,python,Python,我正在创建一个元组,将其转换为字符串,然后使用打印格式重新组织字符串。”“other”有时可以有两个名称,因此我使用了*和“”。在该函数中加入(other): def strFormat(x): #Convert to string s=' ' s = s.join(x) print(s) #Split string into different parts payR, dep, sal, *other, surn = s.split()

我正在创建一个元组,将其转换为字符串,然后使用打印格式重新组织字符串。”“other”有时可以有两个名称,因此我使用了
*
“”。在该函数中加入(other)

def strFormat(x):

    #Convert to string
    s=' '
    s = s.join(x)
    print(s)

    #Split string into different parts
    payR, dep, sal, *other, surn = s.split()
    payR, dep, sal, " ".join(other), surn

    #Print formatting!
    print (surn , other, payR, dep, sal) 
问题是它在字符串中打印“其他”列表,如下所示:

Jones ['David', 'Peter'] 84921 Python 63120
Jones David Peter 84921 Python 63120
Jones, David Peter      84921 Python      £63120
但我希望它更像这样:

Jones ['David', 'Peter'] 84921 Python 63120
Jones David Peter 84921 Python 63120
Jones, David Peter      84921 Python      £63120
这样就可以将其格式化为如下内容:

Jones ['David', 'Peter'] 84921 Python 63120
Jones David Peter 84921 Python 63120
Jones, David Peter      84921 Python      £63120

我这样做对吗?如何阻止列表出现在字符串中?

你很接近了。更改此行(不执行任何操作):


你很接近。更改此行(不执行任何操作):


谢谢它现在可以工作了,你能给我解释一下other=”“.join(other)有什么不同吗?只是想知道,这样我就可以从中学习,谢谢@RJB您没有将
.join
操作的结果分配回变量。返回的字符串不见了,谢谢!它现在可以工作了,你能给我解释一下other=”“.join(other)有什么不同吗?只是想知道,这样我就可以从中学习,谢谢@RJB您没有将
.join
操作的结果分配回变量。返回的字符串消失在任何地方。