pythonで長い文字列を分割して書く方法

普通に+で連結

# coding=utf-8
s = "abc" +\
    "def" +\
    "ghi" +\
    "あいうえお"

assert s == "abcdefghiあいうえお"


%記法

s = "%s%s%s%s" % ("abc", "def", "ghi", "あいうえお")

assert s == "abcdefghiあいうえお"


format

s = "{0}{1}{2}{3}".format("abc", "def", "ghi", "あいうえお")

assert s == "abcdefghiあいうえお"


()で囲んでカンマ無しで文字列を複数書くと1つの文字列として評価される

# coding=utf-8
s = ("abc"
     "def"
     "ghi"
     "あいうえお")

assert s == "abcdefghiあいうえお"


うっかりカンマをつけるとタプルになるので注意

# coding=utf-8

t = ("abc"
     "def",
     "ghi"
     "あいうえお")

assert t == ("abcdef",
             "ghiあいうえお")


一応ベンチマークとってみた

# coding=utf-8
from benchmarker import Benchmarker

c = 100000
with Benchmarker(width=20) as bm:
    with bm('+で連結'):
        for _ in xrange(c):
            s = "abc" + \
                "def" + \
                "ghi" + \
                "あいうえお"

    with bm('()で連結'):
        for _ in xrange(c):
            s = ("abc"
                 "def"
                 "ghi"
                 "あいうえお")

    with bm('%で連結'):
        for _ in xrange(c):
            s = "%s%s%s%s" % ("abc", "def", "ghi", "あいうえお")

    with bm('formatで連結'):
        for _ in xrange(c):
            s = "{0}{1}{2}{3}".format("abc", "def", "ghi", "あいうえお")


内部でどうなってるかわからないけど()で文字列を続けて書くのが一番速い

##                       user       sys     total      real
+で連結             0.0200    0.0000    0.0200    0.0183
()で連結            0.0100    0.0000    0.0100    0.0088
%で連結             0.0400    0.0000    0.0400    0.0399
formatで連結        0.0800    0.0000    0.0800    0.0757

## Ranking               real
()で連結            0.0088 (100.0%) *************************
+で連結             0.0183 ( 47.8%) ************
%で連結             0.0399 ( 22.0%) *****
formatで連結        0.0757 ( 11.6%) ***

## Ratio Matrix          real    [01]    [02]    [03]    [04]
[01] ()で連結       0.0088  100.0%  209.2%  455.4%  864.8%
[02] +で連結        0.0183   47.8%  100.0%  217.7%  413.3%
[03] %で連結        0.0399   22.0%   45.9%  100.0%  189.9%
[04] formatで連結   0.0757   11.6%   24.2%   52.7%  100.0%