3

It sounds like a beginner question, but I never succeeded in writing long strings in a clean way in Python.

Here are the 4 ways I listed to do it. None of them seems ok to me.

def useless_func():
    # WRONG WAY A : string_A displays well but breaks the 80 char max PEP 8 recommandation
    string_A = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

    # WRONG WAY B : string_B will create unwanted spaces between word 'sed' and 'do' when printed
    string_B = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed\
        do eiusmod tempor incididunt ut labore et dolore magna aliqua."

    # WRONG WAY C : string_C displays well  but makes my code ugly because it breaks indentation
    string_C = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed\
do eiusmod tempor incididunt ut labore et dolore magna aliqua."

    # WRONG WAY D : string_D (triples quotes) has the same problem than string_B (unwanted spaces)
    string_D = '''Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
        do eiusmod tempor incididunt ut labore et dolore magna aliqua.'''

Am I missing something?

David Dahan
  • 10,576
  • 11
  • 64
  • 137

4 Answers4

13

I would go with:

def pr():
    # parentheses are for grouping and (as a bonus) for a pretty indentation
    s = ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
         "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
    print s

To quote an informal introduction to Python:

Two string literals next to each other are automatically concatenated; the first line above could also have been written word = 'Help' 'A'; this only works with two literals, not with arbitrary string expressions.

>>> s = 'a' 'b'
>>> s
'ab'
>>> s = 'a''b' # space is not necessary
>>> s
'ab'

A side note: the concatenation is performed during the compilation to bytecode:

>>> import dis
>>> dis.dis(pr)

 0 LOAD_CONST               1 ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')

Probably this concatenation tradition comes from C:

// prints "hello world"
#include <stdio.h>

int main(int argc, char **argv) {
  printf("hello" " world");
  return 0;
}
Nigel Tufnel
  • 11,146
  • 4
  • 35
  • 31
5

You can try:

string = "sdfsdfsdfsdfsdf" \
         "sdfsdfsdfsdfs"

And the result:

>>> string
'sdfsdfsdfsdfsdfsdfsdfsdfsdfs'

The same effect can be achieved by using paranthesis instead of \, as @Nigel Tufnel mentioned in his answer.

linkyndy
  • 17,038
  • 20
  • 114
  • 194
1

What about using double or single triple quote:

>>> string_A = """Lorem ipsum dolor sit amet,
... this is also my content
... this is also my content
... this is also my content"""
>>>
>>> print string_A
Lorem ipsum dolor sit amet,
this is also my content
this is also my content
this is also my content
>>>
James Sapam
  • 16,036
  • 12
  • 50
  • 73
0

I think it boils down to how much screen real-estate you are afforded.

You could use concatenation..

string_a = "this is my content"
string_a += "this is also my content"
askrich
  • 598
  • 5
  • 20