简体   繁体   English

列出字符串并打印格式

[英]List within a string and print formatting

I am creating something which takes a tuple, converts it into a string and then reorganises the string using print formatting. 我正在创建一个采用元组的东西,将其转换为字符串,然后使用打印格式重新组织该字符串。 'other' can sometimes have 2 names, hence why I have used * and the " ".join(other) in this function: “其他”有时可以有2个名称,因此为什么在此函数中使用*" ".join(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) 

The problem with this is that it prints a list of 'other' within the string like this: 问题是它在字符串中打印“ other”的列表,如下所示:

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

But I want it more like this: 但我希望它更像这样:

Jones David Peter 84921 Python 63120

So that it is ready for formatting into something like this: 这样就可以将其格式化为以下格式:

Jones, David Peter      84921 Python      £63120

Am I going about this the right way and how do I stop the list appearing within the string? 我要这样做是正确的方法,如何停止列表出现在字符串中?

You're close. 你近了 Change this line (which does nothing): 更改此行(不执行任何操作):

payR, dep, sal, " ".join(other), surn

to

other = " ".join(other)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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