String concatenation: adding a large patron of text to Python code

Suppose I have a large chunk of text, say

The third and final twist of the Himalayan pilgrimage explores the theme of Sacred Space with a pair of magnificent large mandala paintings, two-dimensional representations of a three-dimensional architectural space in which a particular deity resides. Dating from the fourteenth and sixteenth centuries, these paintings represent the vibrant colors, cosmology of the deity Hevajra. Several other paintings in plain sight depict the historical teachers of various Tibetan orders.

In Java, I can write it as

"The third and final rotation of Himalayan Pilgrimage explores "
+ "the theme of Sacred Space with a pair of magnificent large "
+ "mandala paintings, two-dimensional representations of a "
+ "three-dimensional architectural space where a specific "
+ "deity resides. Dating to the fourteenth and sixteenth "
+ "centuries, these paintings represent, in vivid colors, "
+ "a cosmology of the deity Hevajra. Several other paintings"
+ " on view depict historic teachers of various Tibetan orders."

      

In Python, however, if I do the same, I get complaints about the plus signs +

. If I use instead '''

, I end up with a bunch of leading whitespace due to the indentation (indentation so the code is easy to read).

Does anyone know a solution to this problem: how to insert a large chunk of text into Python code without spaces?

The answer I'm looking for is not this: put all the text on one line

Again, I need to add text that spans more than one line with no extra space.

+3


source to share


3 answers


When using a triple quote line, you don't need to indent:

class SomeClass(object):
    def somemethod(self):
        return '''\
This text
does not need to be indented
at all.
In this text, newlines are preserved.
'''
        # but do continue the next line at the right indentation.

      

You can also auto-join lines using brackets:

foo = (
    "this text will be "
    "joined into one long string. "
    "Note that I don't need to concatenate these "
    "explictly. No newlines are included\n"
    "unless you insert them explicitly."
)

      

because python automatically concatenates sequential strings in a single expression (see String literal concatenation ).



You can still use characters +

to concatenate strings explicitly, but use parentheses to do so in one of the expressions:

foo = (
    "this text will be " +
    "joined into one long string. " + 
    "It is concatenated " +
    "explictly using the `+` operator."
)

      

An alternative would be to use a backslash before the end of the line:

foo = "This is not " \
    "recommended"

      

but I find the use of parentheses and string literal concatenation is more readable.

+12


source


Several ways to do this, when you have multiple lines with nothing but spaces in between, the compiler creates one line from them, as already said. You can also use \

to escape from the end of a line eg.

SomeText="Something" \
         "Something else"

      

or

SomeText="Something" + \
         "Something else"

      



The downside to this is that you have to remember \

on every line. Generally, using +

to concatenate many strings together is a bad idea, because it makes a copy of the string for every one it finds +

, because strings are immutable, which makes it take a long time. Use str.join

like this instead .

SomeText="\n".join(["Something",
                  "Something else",
                  "Last Item"])

      

Note that this has the added benefit that you can replace " "

with some other delimiter depending on what you are doing (newline or no character).

0


source


textwrap.dedent

will clear those leading spaces without messing up the original indentation.

0


source







All Articles