简体   繁体   中英

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.

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?

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:

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

Or a single print using sep :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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