我正在创建一个包含回车符的字符串,并且该字符串太长而不能放在一行(从样式角度来看)。为了在每一行的开头都没有空格,我必须删除缩进。此功能正常,但由于该功能下不再缩进代码,因此看起来很可怕。有没有办法做到这一点,保持缩进并且没有空格?
# report final results
report_profit = f'\n\
Financial Analysis \n\
-------------------------------------------\n\
Total Months: {self.month_count}\n\
Total Profit: ${"{:,.0f}".format(self.profit)}\n\
Average Change: ${"{:,.2f}".format(self.average_diff)}\n\
Positive Change: {self.greatest_dif}\n\
Negative Change: {self.worst_dif}'
您可以创建一个用三引号引起来的字符串,该字符串将存储换行符,制表符等,并以您创建时使用的格式将语句打印出来。
report = f'''
Finacial analysis
--------------------------------
Total Months: Test
Total Profit: Test
Average Change: Test
Positive Change: Test
Negative Change: Test'''
print(report)
我让它像这样工作:
...
....
report_profit = f'\n' +\
'Financial Analysis \n' +\
'-------------------------------------------\n' +\
f'Total Months: {self.month_count}\n' +\
f'Total Profit: ${"{:,.0f}".format(self.profit)}\n' +\
f'Average Change: ${"{:,.2f}".format(self.average_diff)}\n' +\
f'Positive Change: {self.greatest_dif}\n' +\
f'Negative Change: {self.worst_dif}'