因此,正如您可以从标题中猜测的那样,我希望同时打印多个项,无论是变量还是f-字符串,也就是不需要单独打印每个项目。
print(
f'Revenue as stated in the General Journal is ${a}',
f'Revenue as stated in the Transaction Table is ${b}',
f'Revenue is overstated by ${c}',
rt.info(),
rtrtl.info(),
rt.head(10),
rtrtl.head(10)
)当前输出
“普通日刊”所述收入为2,079,839.55美元-如交易表所述,收入为2,238,120.00美元,多报158,280.45美元
所需的输出
“普通日刊”所列收入为2 079 839.55美元
交易表中所列收入为2,238,120.00美元
收入多报158,280.45美元
发布于 2021-11-17 20:16:48
如果需要新行,请在fstring末尾添加'\n‘,或者使用sep=''或使用sep='\n'。此外,要在输出中用逗号格式化浮点数,需要在f-string组件中添加“,”作为格式修饰符。
a = 2_079_839.55
b = 2_238_120.00
c = 158_280.45
print(
f'Revenue as stated in the General Journal is ${a:,}',
f'Revenue as stated in the Transaction Table is ${b:,}',
f'Revenue is overstated by ${c:,}', sep='\n\n')输出:
Revenue as stated in the General Journal is $2,079,839.55
Revenue as stated in the Transaction Table is $2,238,120.0
Revenue is overstated by $158,280.45发布于 2021-11-17 20:21:07
假设print()-ing是循环中的值;如果没有其他arg,它们将转到stdout,因为stdout已经被缓冲,因此没有显式flush=True的连续print()调用与一个大调用没有实际区别。
lines = (
f'Revenue as stated in the General Journal is ${a}',
f'Revenue as stated in the Transaction Table is ${b}',
f'Revenue is overstated by ${c}',
rt.info(),
rtrtl.info(),
rt.head(10),
rtrtl.head(10)
)
for line in lines:
print(line)发布于 2021-11-17 20:25:49
您可以在F字符串中使用三重引用,例如::
print(
f"""Revenue as stated in the General Journal is ${a}
Revenue as stated in the Transaction Table is ${b}
Revenue is overstated by ${c}"""
)https://stackoverflow.com/questions/70010941
复制相似问题