473,396 Members | 1,655 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,396 software developers and data experts.

Trying to write comments after escaping newline

Dear all,

Weirdly enough i do not think I have come across this before. Perhaps I do
not use enough comments....

mystring = "hello " + someVariable + \
"multiple lines 4 clarity" + \
someothervariable

print mystring

The above works , but if I add comments

mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariable #comment

it stops working because instead of escaping a newline I am now just
escaping a space or tab.
Is there some clever thing i have missed?

Thanks for any advice

paul
Jul 18 '05 #1
3 1571
p brian wrote:
mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariable #comment

it stops working because instead of escaping a newline I am now just
escaping a space or tab.
Is there some clever thing i have missed?


Yeah, don't do that. :-) Do this instead;

mystring = ("hello " + someVariable + # comment
"multiple lines 4 clarity" + # another comment
someOtherVariable) # last comment

Better yet, consider finding a way to write your code that doesn't
require comments in the middle of strings like that. It's not
necessarily a good idea to write comments just for comments' sake.
Try going for "self-documenting" code.

Finally, you could also just use string substitution to clean
this stuff up even more:

mystring = "hello %s multiple lines not needed %s" % (someVar, someOtherVar)

That's often easier on the eyes and the fingers than all those
concatenation operators.

-Peter
Jul 18 '05 #2
p brian wrote on Tue, 27 Apr 2004 19:38:14 +0100:
Weirdly enough i do not think I have come across this before. Perhaps I do
not use enough comments....

mystring = "hello " + someVariable + \
"multiple lines 4 clarity" + \
someothervariable
Concatenating strings is not very efficient, you should avoid it.
The above works , but if I add comments

mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariable #comment

it stops working because instead of escaping a newline I am now just
escaping a space or tab.


'\' is not an escape char if it's not inside a string - and it isn't in
this case, since there's no quotes around it. Plus you can't escape
spaces/tabs anyway, since "\ " simply means "backslash followed by space",
which when printed shows up as '\\ '.

In this case it's a line continuation character, meaning that you're saying
"this line ends here, but pretend it continues on the next one"... and then
you try to fool the parser by not making that line end there after all.

A different way to do this could be:

mystring = "hello %s multiple lines 4 clarity %s" % \
(somevariable, someothervariable)

--
Yours,

Andrei

=====
Real contact info (decode with rot13):
ce******@jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
Jul 18 '05 #3
p brian wrote:
mystring = "hello " + someVariable + \
"multiple lines 4 clarity" + \
someothervariable

The above works , but if I add comments

mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariable #comment

it stops working because instead of escaping a newline I am now just
escaping a space or tab.
Is there some clever thing i have missed?


To make line continuation work, the newline at the end of an line has to be
escaped using an \ - and putting your comment there makes this exactly what
you already observed: a space or tab escape instead of the newline.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ChrisC | last post by:
if I had outfile.write((char*) record, sizeof( record )) how would I get write to return a newline? as it is it doesn't. -- ChrisC - chrispche@nospam.yahoo.co.uk
4
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400,...
9
by: Hendrik Wendler | last post by:
Hi everybody, this may be a stupid question: i want to strip comments from a .cpp file. cpp comments look like: // (two slashes) .... comment until newline -->|
4
by: Jeff Rodriguez | last post by:
Main just loops over this while it's not null. The segfault occurs at this line: *line = (char)ch; Also, please don't just fix the code. I would like to know why exactly this isn't working so I...
20
by: drM | last post by:
Could anyone please offer some advice: Trying to play with character arrays in C, the code below generates this error. Anyone see something wrong? thank you in advance. >>>>>>>> #include...
11
by: Michael Powe | last post by:
How can I make an XHTML-compliant form of an expression in this format: document.write("<scr"+"ipt type='text/javascript' src='path/to/file.js'>"+"</scr"+"ipt>"); this turns out to be a...
1
by: George Sakkis | last post by:
The tokenize.generate_tokens function seems to handle in a context- sensitive manner the new line after a comment: .... # hello world .... x = ( .... # hello world .... ) .... ''' .... ...
7
by: wannymahoots | last post by:
optparse seems to be escaping control characters that I pass as arguments on the command line. Is this a bug? Am I missing something? Can this be prevented, or worked around? This behaviour...
6
by: Ed Leafe | last post by:
I've noticed an odd behavior with compile() and code that does not contain a trailing newline: if the last line is a comment inside of any block, a syntax error is thrown, but if the last line is a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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,...

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.