python - 使用 f 字符串生成打印
问题描述
假设我有:
text = ['Apple', 'Banana']
我想使用 f-string 来打印项目。我的预期输出是:
items are:
Apple
Banana
到目前为止,我尝试过:
print(f'items are: {i for i in text}')
这打印:items are: <generator object <genexpr> at 0x0BBCB660>
我怎样才能达到预期的输出?请注意,我也需要反斜杠。
解决方案
text = ['Apple', 'Banana']
nl = '\n'
print(f'items are: {nl}{nl.join(text)}')
输出:
items are:
Apple
Banana
我没有在 f-string 中使用 \n 的原因是因为你不能在 fstrings 中使用反斜杠。
推荐阅读
- oracle-sqldeveloper - SQL 开发人员中复制/迁移/导出之间的区别
- mysql - 使用 Take() to Mysql 的 Linq 查询不会对实际查询添加限制
- python-3.x - 如何在 Python Pandas 中合并另一个数据框中的特定列?
- c - 这个shellcode是什么意思?
- vba - 如何根据当前视图获取活动文件夹中前 X 项
- javascript - 在 PDF.js 中加载 pdf
- python - 异常处理,SyntaxError Vs ValueError Vs Default Exeption
- javascript - 查找所有元素并附加事件侦听器
- css - 何时使用 max-width: none 作为块?
- android-studio - 如何在 Android Studio 中正确导入新模块