Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 – print list separated with \n inside an f-string [duplicate]

I'd like to print a message with a list separated by \n inside an f-string in python3.

my_list = ["Item1", "Item2", "Item3"]
print(f"Your list contains the following items:\n\n{my_list}")

Desired output:

# Your list contains the following items:
#    Item1
#    Item2
#    Item3
like image 288
Yaakov Bressler Avatar asked Jun 18 '26 07:06

Yaakov Bressler


1 Answers

One possible solution, chr(10) evaluates to newline:

my_list = ["Item1", "Item2", "Item3"]
print(f"Your list contains the following items:\n{chr(10).join(my_list)}")

Prints:

Your list contains the following items:
Item1
Item2
Item3
like image 105
Andrej Kesely Avatar answered Jun 19 '26 20:06

Andrej Kesely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!