Re: multiline strings and proper indentation/alignment



Gary John Salerno wrote:

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.


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


.



Relevant Pages

  • Re: cooking the spaghetti
    ... The wise user includes a comment in his original which specifies ... how to set up the tabs. ... and I'll run your code through indent before I try to read it (and ... with the tabstop settings adjusted as necessary. ...
    (comp.lang.c)
  • Re: Tabs bad (Was: ANN: Dao Language v.0.9.6-beta is release!)
    ... The author doesn't mention a single problem that happens because of using tabs. ... "and that it's possible to look at a file and know what interpretation of the TAB character was used, ... is meant to be a single 8-space indent, two 4-space indents, or even eight 1-space indents? ... For the record, I started using tabs, shifted to using spaces because it was "recommended", got frustrated with having to hit multiple key presses to indent and deindent (I use lots of different editors, ranging from kwrite to gedit to nano and even, may Wodan help me, Windows Notepad -- not only do some editors *not* allow auto conversion of tabs, but the ones that do are annoyingly inconsistant in how -- and whether -- they work), went back to tabs, got frustrated with people complaining that indentation was being mangled by various webmail and News clients, and went back to spaces again. ...
    (comp.lang.python)
  • [vim] Indenting with a mixture of tabs and spaces
    ... The PHP coding standards at my workplace state that we should use tabs ... I am trying to get vim to indent code like this automatically. ...
    (comp.editors)
  • Re: Coding best practices
    ... SB> Your name came up in "Perl Best Practices" (along with many ... hard tabs is the ... indent as long as you stick to a clean indent style and enable the ... >> or need syntax highlighting or use debuggers other than print. ...
    (perl.beginners)
  • Re: reformat to tool/editor-compliant C style?
    ... no. *remove* all the tabs. ... little reason to expect any program to be able to indent it right. ... handle foo; ... } else if (bar) { ...
    (comp.lang.c)