如何在python中拆分长的f字符串? [英] How to split up a long f-string in python?

查看:93
本文介绍了如何在python中拆分长的f字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了line too long pep8 E501问题.

I am getting a line too long pep8 E501 issue.

f'Leave Request created successfully. Approvers sent the request for approval: {leave_approver_list}'

我尝试使用多行字符串,但这带来了\n,这破坏了我的测试:

I tried using a multi-line string, but that brings in a \n, which breaks my test:

f'''Leave Request created successfully.
Approvers sent the request for approval: {leave_approver_list}'''

如何将其保持单行并通过pep8棉绒

How can I keep it single line and pass pep8 linting

推荐答案

除非您将字符串包装在括号内,否则将需要换行.在这种情况下,f将需要放在第二行之前:

You will need a line break unless you wrap your string within parentheses. In this case, f will need to be prepended to the second line:

'Leave Request created successfully.'\ 
f'Approvers sent the request for approval: {leave_approver_list}'

这是一个小演示:

In [97]: a = 123

In [98]: 'foo_'\
    ...: f'bar_{a}'
Out[98]: 'foo_bar_123'

我建议juanpa回答,因为它更干净,但这是实现此目的的一种方法.

I recommend juanpa's answer since it is cleaner, but this is one way to do this.

这篇关于如何在python中拆分长的f字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆