473,416 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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.
May 10 '06 #1
9 3152
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rasmus Fogh | last post by:
Dear All, I need a way of writing strings or arbitrary Python code that will a) allow the strings to be read again unchanged (like repr) b) write multiline strings as multiline strings instead...
8
by: Christoph Zwerschke | last post by:
I sometimes use triple quotes in order to produce snippets of multiline code, like that: if output == html: snip = '''<html> <head><title>Hello, World</title></head> <body...
2
by: rick_muller | last post by:
I'm trying to embed a Python interpreter in a GUI I'm developing, and I'm having trouble understanding the proper use of code.InteractiveInterpreter. Here's what I'm trying: % python Python...
25
by: sravishnu | last post by:
Hello, I have written a program to concatanae two strings, and should be returned to the main program. Iam enclosing the code, please give me ur critics. Thanks, main() { char s1,s2;...
40
by: Edward Elliott | last post by:
At the risk of flogging a dead horse, I'm wondering why Python doesn't have any multiline comments. One can abuse triple-quotes for that purpose, but that's obviously not what it's for and doesn't...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
6
by: Zdenek Maxa | last post by:
Hi all, I would like to perform regular expression replace (e.g. removing everything from within tags in a XML file) with multiple-line pattern. How can I do this? where =...
0
by: yogarajan | last post by:
hi all i need proper alignment my treeview looks like <checkbox>Fruits(parent)-134 <checkbox>Mango(child)-45 <checkbox>Orange(child)-45 ...
7
by: Anil Gupte | last post by:
I have read a lot about getting lines from a multiline textbox in VB.Net. However, I cannot for the life of me figure out how to write to a multiline textbox. Basically, I have created an array of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.