正确缩进Python多行字符串

函数中Python多行字符串的正确缩进是什么?


    def method():

        string = """line one

line two

line three"""

要么


    def method():

        string = """line one

        line two

        line three"""

或者是其他东西?


在第一个示例中,将字符串挂在函数外部看起来有些奇怪。


海绵宝宝撒
浏览 1194回答 3
3回答

缥缈止盈

您可能想与 """def foo():&nbsp; &nbsp; string = """line one&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;line two&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;line three"""由于换行符和空格包含在字符串本身中,因此您必须对其进行后处理。如果您不想这样做,并且文本很多,则可能需要将其分别存储在文本文件中。如果文本文件不能很好地适合您的应用程序,并且您不想进行后处理,我可能会选择def foo():&nbsp; &nbsp; string = ("this is an "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "implicitly joined "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "string")如果要对多行字符串进行后处理以修剪掉不需要的部分,则应考虑PEP 257中textwrap介绍的对文档字符串进行后处理的模块或技术:def trim(docstring):&nbsp; &nbsp; if not docstring:&nbsp; &nbsp; &nbsp; &nbsp; return ''&nbsp; &nbsp; # Convert tabs to spaces (following the normal Python rules)&nbsp; &nbsp; # and split into a list of lines:&nbsp; &nbsp; lines = docstring.expandtabs().splitlines()&nbsp; &nbsp; # Determine minimum indentation (first line doesn't count):&nbsp; &nbsp; indent = sys.maxint&nbsp; &nbsp; for line in lines[1:]:&nbsp; &nbsp; &nbsp; &nbsp; stripped = line.lstrip()&nbsp; &nbsp; &nbsp; &nbsp; if stripped:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indent = min(indent, len(line) - len(stripped))&nbsp; &nbsp; # Remove indentation (first line is special):&nbsp; &nbsp; trimmed = [lines[0].strip()]&nbsp; &nbsp; if indent < sys.maxint:&nbsp; &nbsp; &nbsp; &nbsp; for line in lines[1:]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trimmed.append(line[indent:].rstrip())&nbsp; &nbsp; # Strip off trailing and leading blank lines:&nbsp; &nbsp; while trimmed and not trimmed[-1]:&nbsp; &nbsp; &nbsp; &nbsp; trimmed.pop()&nbsp; &nbsp; while trimmed and not trimmed[0]:&nbsp; &nbsp; &nbsp; &nbsp; trimmed.pop(0)&nbsp; &nbsp; # Return a single string:&nbsp; &nbsp; return '\n'.join(trimmed)

呼啦一阵风

该textwrap.dedent功能允许在源代码中以正确的缩进开始,然后在使用前从文本中删除它。正如其他一些人所指出的那样,折衷方案是这是对文字的一个额外的函数调用。在决定将这些文字放在代码中的位置时,请考虑到这一点。import textwrapdef frobnicate(param):&nbsp; &nbsp; """ Frobnicate the scrognate param.&nbsp; &nbsp; &nbsp; &nbsp; The Weebly-Ruckford algorithm is employed to frobnicate&nbsp; &nbsp; &nbsp; &nbsp; the scrognate to within an inch of its life.&nbsp; &nbsp; &nbsp; &nbsp; """&nbsp; &nbsp; prepare_the_comfy_chair(param)&nbsp; &nbsp; log_message = textwrap.dedent("""\&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Prepare to frobnicate:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Here it comes...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Any moment now.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; And: Frobnicate!""")&nbsp; &nbsp; weebly(param, log_message)&nbsp; &nbsp; ruckford(param)\日志消息文字中的结尾是为了确保换行符不在文字中;这样,文字不以空白行开头,而是以下一个完整行开头。从的返回值textwrap.dedent是输入字符串,在字符串的每一行上都删除了所有常见的前导空格。因此,上面的log_message值将是:Prepare to frobnicate:Here it comes...&nbsp; &nbsp; Any moment now.And: Frobnicate!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python