multiline strings and proper indentation/alignment

J

John Salerno

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.
 
C

Christoph Haas

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
 
S

Scott David Daniels

John said:
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
(e-mail address removed)
 
J

John Salerno

Scott said:
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
(e-mail address removed)

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!)
 
G

Gary Herron

Gary said:
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
 
J

John Salerno

Gary said:
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?
 
B

bruno at modulix

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

Why not trying by yourself ?-).... this is a multiline
.... triple-quted string with
.... indentation for nicer code formatting
.... """
this is a multiline
triple-quted string with
indentation for nicer code formatting

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

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

Mmm. Not good. Let's try again: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
 
J

John Salerno

bruno said:
Why not trying by yourself ?-)

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

Mmm. Not good. Let's try again:
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.
 
D

Dave Hansen

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



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:
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
 
J

John Salerno

Dave said:
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.
 
D

Dennis Lee Bieber

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

I suspect you are going to have to give up one or the other....
Either the "internal" formatting, or the source formatting...
(Especially since HTML, itself, ignores the formatting except inside
<pre></pre> tags.)

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

-=-=-=-=-=-=-=-=-
tmp = ( '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML4.01//EN"\n'
'"http://www.w3.org/TR/html4/strict.dtd">\n\n'
'<HTML>\n\t<HEAD>\n\t</HEAD>\n'
'\t<BODY>\n\n\n\t</BODY>\n</HTML>'
)

print tmp
-=-=-=-=-=-=-=-=-

(Those do line up under a fixed-pitch typeface)

-=-=-=-=-=-=-=-=-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<HTML>
<HEAD>
</HEAD>
<BODY>


</BODY>
</HTML>


Granted, you now have to worry about matching up quotes safely <G>
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Dave Hansen

But I have some newlines that are already embedded in the string, and I
wouldn't want those replaced.
I want the following line-
concatenated, but leave this
line break alone.
"""I want the following line concatenated, but leave this
line break alone.
But I'd still recommend using parens and string concatentation.
"I want the following line "
"concatentated, but leave this\n"
"line break alone."
)I want the following line concatentated, but leave this
line break alone.

Regards,
-=Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top