Python 如何包装使用格式说明符/占位符的很长字符串?

Python 如何包装使用格式说明符/占位符的很长字符串?,python,Python,我知道你会渴望将此标记为重复,但区别在于我使用的是格式占位符 原始行 print(f"There are {rangeSegment} numbers between {rangeStart} and {rangeEnd} inclusively blah blah blah.") 在StackOverflow上使用已接受的PEP8建议和已接受的答案建议使用隐含的连接,但这会产生带有制表符的输出 print(f"There are {rangeSegment} n

我知道你会渴望将此标记为重复,但区别在于我使用的是格式占位符

原始行

print(f"There are {rangeSegment} numbers between {rangeStart} and {rangeEnd} inclusively blah blah blah.")
在StackOverflow上使用已接受的PEP8建议和已接受的答案建议使用隐含的连接,但这会产生带有制表符的输出

print(f"There are {rangeSegment} numbers between {rangeStart} and " \
    "{rangeEnd} inclusively.")
输出

There are 10 numbers between 1 and     10 inclusively.
There are 10 numbers between 1 and {rangeEnd} inclusively.
尝试拆分多个引号会破坏字符串格式

print(f"There are {rangeSegment} numbers between {rangeStart} and" \
    "{rangeEnd} inclusively.")
输出

There are 10 numbers between 1 and     10 inclusively.
There are 10 numbers between 1 and {rangeEnd} inclusively.

在要从中开始下一行的字符串中添加\n请尝试以下操作:

print(f"There are {rangeSegment} numbers between {rangeStart} and " \
          f"{rangeEnd} inclusively.")

您需要为两个stings设置
f

您的大部分功能正常。您只需在打印语句的每一行之前使用
f

rangeSegment = 20
rangeStart = 2
rangeEnd = 15

print(f"There are {rangeSegment} numbers between {rangeStart} and " \
      f"{rangeEnd} inclusively.") \
      f" I am going to have another line here {rangeStart} and {rangeEnd}." \
      f" One last line just to show that i can print more lines.")
上述声明将打印以下内容:

There are 20 numbers between 2 and 15 inclusively. I am going to have another line here 30 and 40. One last line just to show that i can print more lines.
请注意,如果要在中间断开这一行,则必须在您认为需要断开的位置使用
\n

例如,如果打印语句如下所示:

print(f"There are {rangeSegment} numbers between {rangeStart} and " \
    f"{rangeEnd} inclusively.\n"  \
    f"I am going to have another line here {rangeStart} and {rangeEnd}\n" \
    f"One last line just to show that i can print more lines")
然后,您的输出将如下所示。
\n
将创建新行

There are 20 numbers between 30 and 40 inclusively.
I am going to have another line here 30 and 40
One last line just to show that i can print more lines

只需在两个字符串文本上都加上f前缀。无法将制表符显示/投票关闭为打字错误。