我正在创建一个从函数返回的字符串。我需要字符串来列出一些值。
我创建了一个列表,该列表在末尾连接以形成完整的字符串。我试图在列表附加到的 while 循环中的每次迭代中添加一个新行。
while shiftAmount < 25: #Iterates through all 26 combinations producing a processed value each time.
shiftAndStore(shiftAmount)
shiftAmount = shiftAmount + 1
stringshift = str(shiftAmount)
outputCipherText.append("The Encoded / Decoded text on shift " + str((shiftAmount - 1)) + " is ")
outputCipherText.append(str(''.join(completePlainText)) + " \n")
completePlainText = []
output = [''.join(outputCipherText)]
谁能告诉我为什么这不起作用?查看输出,它显示\n 但不执行它并创建一个新行。
最佳答案
问题是您没有打印字符串,而是打印字符串列表。线路
output = [''.join(outputCipherText)]
正在将单个字符串添加到列表中。这对我来说看起来没什么用,你可以简单地改变它:
output = ''.join(outputCipherText)
获取一个字符串,而不是仅包含一个字符串的列表。更改后,您可以使用打印输出
,并且所有特殊字符都将执行,因此\n
字符将显示为换行符。
如果您不想更改代码,则必须打印字符串而不是字符串列表:
print output[0]
关于python - 从列表创建字符串并添加换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13210945/