473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3169
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***********@a cm.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***********@a cm.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.pytho n, John Salerno
<jo******@NOSPA Mgmail.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().rep lace('\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().rep lace('\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.pytho n, John Salerno
<jo******@NOSPA Mgmail.com> wrote:
Dave Hansen wrote:
> print textwrap.dedent (s).strip().rep lace('\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().rep lace('-\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 "
"concatenta ted, 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
1948
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 of escaping the \n's. A repr function that output triple-quoted strings with explicit (non-escaped) linebreaks would be perfect.
8
3223
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 bgcolor="aqua"><h1>What's up?</h1> </html>''' else: snip = 'Hello!'
2
2611
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 2.3 (#1, Sep 13 2003, 00:49:11) on darwin Type "help", "copyright", "credits" or "license" for more information.
25
3894
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; printf("enter first string"); scanf("%s",s1); printf("enter second string");
40
4608
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 nest properly. ML has a very elegant system for nested comments with (* and *). Using an editor to throw #s in front of every line has limitations. Your editor has to support it and you have to know how to use that feature. Not exactly...
135
7439
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 which is better. It has become what's known as “religious war” — a heated fight over trivia. In this essay, i like to explain what is the situation behind it, and which is proper.
6
3790
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 = open("filename").read() multilinePattern = "^<tag.... <\/tag>$" re.search(multilinePattern, where, re.MULTILINE)
0
1267
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 <checkbox>Apple(child)-34 <checkbox>Car(parent)-223
7
15441
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 strings which I want to "paste" into a multiline textbox. Any ideas? -- Anil Gupte www.keeninc.net www.icinema.com
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8631
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.