multiline strings and proper indentation/alignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    multiline strings and proper indentation/alignment

    How do you make a single string span multiple lines, but also allow
    yourself to indent the second (third, etc.) lines so that it lines up
    where you want it, without causing the newlines and tabs or spaces to be
    added to the string as well?

    Example (pretend this is all on one line):

    self.DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML
    4.01//EN"\n"http://www.w3.org/TR/html4/strict.dtd">\n\ n'

    I want it to read:

    self.DTD = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n
    "http://www.w3.org/TR/html4/strict.dtd">\n\ n'''

    Or anything like that, but I don't want the extra newline or tabs to be
    a part of the string when it's printed.

    Thanks.
  • Christoph Haas

    #2
    Re: multiline strings and proper indentation/alignment

    On Tue, May 09, 2006 at 05:38:52PM +0000, John Salerno wrote:[color=blue]
    > How do you make a single string span multiple lines, but also allow
    > yourself to indent the second (third, etc.) lines so that it lines up
    > where you want it, without causing the newlines and tabs or spaces to be
    > added to the string as well?
    >
    > Example (pretend this is all on one line):
    >
    > self.DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML
    > 4.01//EN"\n"http://www.w3.org/TR/html4/strict.dtd">\n\ n'
    >
    > I want it to read:
    >
    > self.DTD = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n
    > "http://www.w3.org/TR/html4/strict.dtd">\n\ n'''
    >
    > Or anything like that, but I don't want the extra newline or tabs to be
    > a part of the string when it's printed.[/color]

    My favorite way:

    self.DTD = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN '''
    '''http://www.w3.org/TR/html4/strict.dtd">\n\ n'''

    Kindly
    Christoph

    Comment

    • Scott David Daniels

      #3
      Re: multiline strings and proper indentation/alignment

      John Salerno wrote:[color=blue]
      > How do you make a single string span multiple lines, but also allow
      > yourself to indent the second ... without causing the newlines and
      > tabs or spaces to be added to the string as well?
      >
      > self.DTD = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n
      > "http://www.w3.org/TR/html4/strict.dtd">\n\ n'''
      >
      > ..., but I don't want the extra newline or tabs to be
      > a part of the string when it's printed.[/color]

      The easiest way:

      self.DTD = ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n'
      '"http://www.w3.org/TR/html4/strict.dtd">\n\ n')

      Adjacent strings are combined at compile-time, and parens around allows
      you to do a multi-line expression.

      --Scott David Daniels
      scott.daniels@a cm.org

      Comment

      • John Salerno

        #4
        Re: multiline strings and proper indentation/alignment

        Scott David Daniels wrote:[color=blue]
        > John Salerno wrote:[color=green]
        >> How do you make a single string span multiple lines, but also allow
        >> yourself to indent the second ... without causing the newlines and
        >> tabs or spaces to be added to the string as well?
        > >
        >> self.DTD = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n
        >> "http://www.w3.org/TR/html4/strict.dtd">\n\ n'''
        >>
        >> ..., but I don't want the extra newline or tabs to be a part of the
        >> string when it's printed.[/color]
        >
        > The easiest way:
        >
        > self.DTD = ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n'
        > '"http://www.w3.org/TR/html4/strict.dtd">\n\ n')
        >
        > Adjacent strings are combined at compile-time, and parens around allows
        > you to do a multi-line expression.
        >
        > --Scott David Daniels
        > scott.daniels@a cm.org[/color]

        Thanks guys. Looks like both of your suggestions are pretty much the
        same thing, which is putting strings next to one another. Something
        about it looks wrong, but I guess it works!)

        Comment

        • Gary Herron

          #5
          Re: multiline strings and proper indentation/alignment

          Gary John Salerno wrote:
          [color=blue]
          >How do you make a single string span multiple lines, but also allow
          >yourself to indent the second (third, etc.) lines so that it lines up
          >where you want it, without causing the newlines and tabs or spaces to be
          >added to the string as well?
          >
          >Example (pretend this is all on one line):
          >
          >self.DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML
          >4.01//EN"\n"http://www.w3.org/TR/html4/strict.dtd">\n\ n'
          >
          >I want it to read:
          >
          >self.DTD = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n
          > "http://www.w3.org/TR/html4/strict.dtd">\n\ n'''
          >
          >Or anything like that, but I don't want the extra newline or tabs to be
          >a part of the string when it's printed.
          >
          >Thanks.
          >
          >[/color]
          The textwrap module has a function to do just the thing you want.

          *dedent*( text)

          Remove any whitespace that can be uniformly removed from the left of
          every line in text.

          This is typically used to make triple-quoted strings
          line up with the left edge of screen/whatever, while still
          presenting it in the source code in indented form.

          Gary Herron


          Comment

          • bruno at modulix

            #6
            Re: multiline strings and proper indentation/alignment

            John Salerno wrote:[color=blue]
            > Gary Herron wrote:
            >[color=green]
            >> Gary John Salerno wrote:
            >>[color=darkred]
            >>> How do you make a single string span multiple lines, but also allow
            >>> yourself to indent the second (third, etc.) lines so that it lines up
            >>> where you want it, without causing the newlines and tabs or spaces to
            >>> be added to the string as well?
            >>>
            >>> Example (pretend this is all on one line):
            >>>
            >>> self.DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML
            >>> 4.01//EN"\n"http://www.w3.org/TR/html4/strict.dtd">\n\ n'
            >>>
            >>> I want it to read:
            >>>
            >>> self.DTD = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"\n
            >>> "http://www.w3.org/TR/html4/strict.dtd">\n\ n'''
            >>>
            >>> Or anything like that, but I don't want the extra newline or tabs to
            >>> be a part of the string when it's printed.
            >>>
            >>> Thanks.
            >>>
            >>>[/color]
            >> The textwrap module has a function to do just the thing you want.
            >> *dedent*( text)
            >>
            >> Remove any whitespace that can be uniformly removed from the left of
            >> every line in text.
            >>
            >> This is typically used to make triple-quoted strings
            >> line up with the left edge of screen/whatever, while still
            >> presenting it in the source code in indented form.
            >>
            >> Gary Herron
            >>
            >>[/color]
            >
            > But does this do anything to the newline character that gets added to
            > the end of the first line?[/color]

            Why not trying by yourself ?-)[color=blue][color=green][color=darkred]
            >>> import textwrap
            >>> s = """[/color][/color][/color]
            .... this is a multiline
            .... triple-quted string with
            .... indentation for nicer code formatting
            .... """[color=blue][color=green][color=darkred]
            >>> print s[/color][/color][/color]

            this is a multiline
            triple-quted string with
            indentation for nicer code formatting
            [color=blue][color=green][color=darkred]
            >>> print textwrap.dedent (s)[/color][/color][/color]

            this is a multiline
            triple-quted string with
            indentation for nicer code formatting
            [color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            Obviously, you have to strip newlines yourself. Let's try:[color=blue][color=green][color=darkred]
            >>> print textwrap.dedent (s.strip())[/color][/color][/color]
            this is a multiline
            triple-quted string with
            indentation for nicer code formatting

            Mmm. Not good. Let's try again:[color=blue][color=green][color=darkred]
            >>> print textwrap.dedent (s).strip()[/color][/color][/color]
            this is a multiline
            triple-quted string with
            indentation for nicer code formatting[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            Well, seems like we're done. About 2'30'' to solve the problem.

            FWIW, reading textwrap's doc may be useful to - no need to reinvent the
            SquaredWheel(tm ) if the rounded version already exists !-)

            HTH
            --
            bruno desthuilliers
            python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
            p in 'onurb@xiludom. gro'.split('@')])"

            Comment

            • John Salerno

              #7
              Re: multiline strings and proper indentation/alignment

              bruno at modulix wrote:
              [color=blue]
              > Why not trying by yourself ?-)[/color]

              Doh! I always forget I can do this! :)

              [color=blue]
              > Mmm. Not good. Let's try again:[color=green][color=darkred]
              >>>> print textwrap.dedent (s).strip()[/color][/color]
              > this is a multiline
              > triple-quted string with
              > indentation for nicer code formatting
              >
              > Well, seems like we're done. About 2'30'' to solve the problem.[/color]

              Actually, I'm still wondering if it's possible to remove the newlines at
              the end of the first and second lines (after 'multiline' and 'with'), so
              that the string is one line. But it's already been shown that textwrap
              alone doesn't do this, so I'd rather not mess with all the extra stuff
              to do it, when I can just put the string in parentheses.

              Thanks.

              Comment

              • Dave Hansen

                #8
                Re: multiline strings and proper indentation/alignment

                On Wed, 10 May 2006 13:56:52 GMT in comp.lang.pytho n, John Salerno
                <johnjsal@NOSPA Mgmail.com> wrote:
                [color=blue]
                >bruno at modulix wrote:
                >[color=green]
                >> Why not trying by yourself ?-)[/color]
                >
                >Doh! I always forget I can do this! :)
                >
                >[color=green]
                >> Mmm. Not good. Let's try again:[color=darkred]
                >>>>> print textwrap.dedent (s).strip()[/color]
                >> this is a multiline
                >> triple-quted string with
                >> indentation for nicer code formatting
                >>
                >> Well, seems like we're done. About 2'30'' to solve the problem.[/color]
                >
                >Actually, I'm still wondering if it's possible to remove the newlines at
                >the end of the first and second lines (after 'multiline' and 'with'), so[/color]

                Well, it's too long for my news reader to display the result on a
                single line, but:
                [color=blue][color=green][color=darkred]
                >>> print textwrap.dedent (s).strip().rep lace('\n',' ')[/color][/color][/color]
                this is a multiline triple-quted string with indentation for nicer
                code formatting[color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]
                [color=blue]
                >that the string is one line. But it's already been shown that textwrap
                >alone doesn't do this, so I'd rather not mess with all the extra stuff
                >to do it, when I can just put the string in parentheses.[/color]

                If that's the way you want the sting in the first place, that'd be my
                recommendation. Regards,
                -=Dave

                --
                Change is inevitable, progress is not.

                Comment

                • John Salerno

                  #9
                  Re: multiline strings and proper indentation/alignment

                  Dave Hansen wrote:
                  [color=blue][color=green][color=darkred]
                  >>>> print textwrap.dedent (s).strip().rep lace('\n',' ')[/color][/color]
                  > this is a multiline triple-quted string with indentation for nicer
                  > code formatting[/color]

                  But I have some newlines that are already embedded in the string, and I
                  wouldn't want those replaced.

                  Comment

                  • Dave Hansen

                    #10
                    Re: multiline strings and proper indentation/alignment

                    On Wed, 10 May 2006 15:50:38 GMT in comp.lang.pytho n, John Salerno
                    <johnjsal@NOSPA Mgmail.com> wrote:
                    [color=blue]
                    >Dave Hansen wrote:
                    >[color=green][color=darkred]
                    >>>>> print textwrap.dedent (s).strip().rep lace('\n',' ')[/color]
                    >> this is a multiline triple-quted string with indentation for nicer
                    >> code formatting[/color]
                    >
                    >But I have some newlines that are already embedded in the string, and I
                    >wouldn't want those replaced.[/color]
                    [color=blue][color=green][color=darkred]
                    >>> s = """[/color][/color][/color]
                    I want the following line-
                    concatenated, but leave this
                    line break alone.
                    """[color=blue][color=green][color=darkred]
                    >>> print textwrap.dedent (s).strip().rep lace('-\n',' ')[/color][/color][/color]
                    I want the following line concatenated, but leave this
                    line break alone.[color=blue][color=green][color=darkred]
                    >>>[/color][/color][/color]

                    But I'd still recommend using parens and string concatentation.
                    [color=blue][color=green][color=darkred]
                    >>> s2 = ([/color][/color][/color]
                    "I want the following line "
                    "concatenta ted, but leave this\n"
                    "line break alone."
                    )[color=blue][color=green][color=darkred]
                    >>> print s2[/color][/color][/color]
                    I want the following line concatentated, but leave this
                    line break alone.

                    Regards,
                    -=Dave

                    --
                    Change is inevitable, progress is not.

                    Comment

                    Working...