Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
跨越多行缩进的python字符串_Python_String_Format_Styles - Fatal编程技术网

跨越多行缩进的python字符串

跨越多行缩进的python字符串,python,string,format,styles,Python,String,Format,Styles,我正在创建包含回车符的字符串,该字符串太长,不能放在一行上(从样式的角度来看)。为了避免每行开头都有空白,我必须删除缩进。这个函数很好,但看起来很糟糕,因为代码不再缩进到函数下。有没有办法做到这一点,保持缩进,而没有空格 # report final results report_profit = f'\n\ Financial Analysis \n\ -------------------------------------------\n\ Total

我正在创建包含回车符的字符串,该字符串太长,不能放在一行上(从样式的角度来看)。为了避免每行开头都有空白,我必须删除缩进。这个函数很好,但看起来很糟糕,因为代码不再缩进到函数下。有没有办法做到这一点,保持缩进,而没有空格

# 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}'

这是一个很好的功能,我将使用它。看起来好多了。但是,我试图使代码与代码中的位置缩进。如果我将其放在类中的函数中,它将以10行缩进打印。另一个选项是忽略缩进,并放置第二行而不缩进。这将在没有缩进的情况下打印,但是完全缩进的代码看起来很松散。如果这能让我的问题更清楚的话