Yaa*_*ler 1 python python-3.x f-string
我想在 python3 的 f 字符串中打印一条消息,其中包含一个由 \n 分隔的列表。
my_list = ["Item1", "Item2", "Item3"]
print(f"Your list contains the following items:\n\n{my_list}")
Run Code Online (Sandbox Code Playgroud)
期望的输出:
# Your list contains the following items:
# Item1
# Item2
# Item3
Run Code Online (Sandbox Code Playgroud)
一种可能的解决方案,chr(10)评估为换行符:
my_list = ["Item1", "Item2", "Item3"]
print(f"Your list contains the following items:\n{chr(10).join(my_list)}")
Run Code Online (Sandbox Code Playgroud)
印刷:
Your list contains the following items:
Item1
Item2
Item3
Run Code Online (Sandbox Code Playgroud)