Use triple quote:
d = """ this is
a sample text
which does
not mean
anything"""
Thanks but what if the string is 500 lines. Seems it would be hard to
put a "\" manually at the end of every line. How could i do that?
That depends. You can put the string in a separate text file and read the file,
or you can have it as a literal. For the latter your editor should provide you
with the tools to format the string any which way you want, and if not, then
just a write a Python script to format it for you.
Consider this little example[1]:
<code file="jabberwocky.py">
"The Jabberwocky poem, by Lewis Carrol"
text = (
"'Twas brillig, and the slithy toves\n"
"Did gyre and gimble in the wabe;\n"
" All mimsy were the borogoves,\n"
" And the mome raths outgrabe.\n"
"\n"
"\"Beware the jabberwock, my son!\n"
" The jaws that bite, the claws that catch!\n"
"Beware the jubjub bird, and shun\n"
" The frumious bandersnatch!\"\n"
"\n"
"He took his vorpal sword in hand:\n"
" Long time the manxome foe he sought--\n"
"So rested he by the tumtum tree,\n"
" And stood awhile in thought.\n"
"\n"
"And as in uffish thought he stood,\n"
" The jabberwock, with eyes of flame,\n"
"Came whiffling through the tulgey wood,\n"
" And burbled as it came!\n"
"\n"
"One, two! one, two! and through and through\n"
" The vorpal blade went snicker-snack!\n"
"He left it dead, and with its head\n"
" He went galumphing back.\n"
"\n"
"\"And hast thou slain the jabberwock?\n"
" Come to my arms, my beamish boy!\n"
"O frabjous day! callooh! callay!\"\n"
" He chortled in his joy.\n"
"\n"
"'Twas brillig, and the slithy toves\n"
"Did gyre and gimble in the wabe;\n"
" All mimsy were the borogoves,\n"
" And the mome raths outgrabe."
)
</code>
This defines /one/ string value, using compile time concatenation (any adjacent
string literals are concatenated at compile time, in Python[2] and in C++).
The text was just copied and pasted from Wikipedia, and subjected to a few well
chosen keystrokes in an editor.
As a hopefully illuminating exercise, consider a Python program that uses this
string (just import the above module and use its 'text') and generates the above
source code as output.
Cheers & hth.,
- Alf
Notes:
[1] From an example at the end of chapter 2 at <url:
http://tinyurl.com/programmingbookP3>.
[2] I'm not sure how well that plays with Python doc strings; haven't tried.