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. 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
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
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!)
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
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('@')])"
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.
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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;...
|
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...
|
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...
|
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 =...
|
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
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |