简体   繁体   English

当有另一个变量时,如何使用print添加新行(Python 3.x)

[英]How to add a new line with print when another variable is in place (Python 3.x)

I am new to learning Python 3.0 and I am trying to solve an issue I have with making a list and printing at the end of it a new line to separate printing between two strings. 我是学习Python 3.0的新手,我正在尝试解决一个问题,该问题涉及制作列表并在列表末尾打印新行,以分隔两个字符串之间的打印。

IE IE浏览器

list_1 = ['Autumn', 'Mary', 'Ditto', 'Gamma']
print(list_1)
print(list_1[2])

Output:
['Autumn', 'Mary', 'Ditto', 'Gamma']
Ditto

I was wondering is it possible I can add a new line in the first print() statement? 我想知道是否可以在第一个print()语句中添加新行?

So I could end up with: 所以我最终会得到:

['Autumn', 'Mary', 'Ditto', 'Gamma']

Ditto

Thank you for any help whatsoever on this question. 感谢您对这个问题的任何帮助。 I really appreciate it! 我真的很感激!

Yes, set end="\\n\\n" to add an extra newline or as many as you want: 是的,将end="\\n\\n"为添加额外的换行符或任意多个:

print(list_1, end="\n\n")
print(list_1[2])

Or a single print using sep : 或使用sep进行单张打印:

print(list_1, list_1[2], sep="\n\n")

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

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