By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,761 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,761 IT Pros & Developers. It's quick & easy.

multiline strings and proper indentation/alignment

P: n/a
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.
May 10 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Tue, May 09, 2006 at 05:38:52PM +0000, 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.


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
May 10 '06 #2

P: n/a
John Salerno wrote:
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.


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
sc***********@acm.org
May 10 '06 #3

P: n/a
Scott David Daniels wrote:
John Salerno wrote:
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.


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
sc***********@acm.org


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!)
May 10 '06 #4

P: n/a
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
May 10 '06 #5

P: n/a
John Salerno wrote:
Gary Herron wrote:
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


But does this do anything to the newline character that gets added to
the end of the first line?


Why not trying by yourself ?-)
import textwrap
s = """ .... this is a multiline
.... triple-quted string with
.... indentation for nicer code formatting
.... """ print s
this is a multiline
triple-quted string with
indentation for nicer code formatting
print textwrap.dedent(s)
this is a multiline
triple-quted string with
indentation for nicer code formatting

Obviously, you have to strip newlines yourself. Let's try: print textwrap.dedent(s.strip()) this is a multiline
triple-quted string with
indentation for nicer code formatting

Mmm. Not good. Let's try again: print textwrap.dedent(s).strip() 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.

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 'o****@xiludom.gro'.split('@')])"
May 10 '06 #6

P: n/a
bruno at modulix wrote:
Why not trying by yourself ?-)
Doh! I always forget I can do this! :)

Mmm. Not good. Let's try again:
print textwrap.dedent(s).strip()

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.


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.
May 10 '06 #7

P: n/a
On Wed, 10 May 2006 13:56:52 GMT in comp.lang.python, John Salerno
<jo******@NOSPAMgmail.com> wrote:
bruno at modulix wrote:
Why not trying by yourself ?-)
Doh! I always forget I can do this! :)

Mmm. Not good. Let's try again:
> print textwrap.dedent(s).strip()

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.


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


Well, it's too long for my news reader to display the result on a
single line, but:
print textwrap.dedent(s).strip().replace('\n',' ') this is a multiline triple-quted string with indentation for nicer
code formatting

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.


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.
May 10 '06 #8

P: n/a
Dave Hansen wrote:
print textwrap.dedent(s).strip().replace('\n',' ')

this is a multiline triple-quted string with indentation for nicer
code formatting


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

P: n/a
On Wed, 10 May 2006 15:50:38 GMT in comp.lang.python, John Salerno
<jo******@NOSPAMgmail.com> wrote:
Dave Hansen wrote:
> print textwrap.dedent(s).strip().replace('\n',' ')

this is a multiline triple-quted string with indentation for nicer
code formatting


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

s = """ I want the following line-
concatenated, but leave this
line break alone.
""" print textwrap.dedent(s).strip().replace('-\n',' ') I want the following line concatenated, but leave this
line break alone.
But I'd still recommend using parens and string concatentation.
s2 = ( "I want the following line "
"concatentated, but leave this\n"
"line break alone."
) print s2

I want the following line concatentated, but leave this
line break alone.

Regards,
-=Dave

--
Change is inevitable, progress is not.
May 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.