开发者

How do I add multiple strings over several lines in Python?

I'm lost in Python world:

m开发者_如何学Cessage = struct.pack('B', 4) +
    minissdpdStringEncode(st) +
    minissdpdStringEncode(usn) +
    minissdpdStringEncode(server) +
    minissdpdStringEncode(location)

It doesn't run. Do I really need to put this all on one line or something?

That would be messy in my opinion.


You have two choices:

message = struct.pack('B', 4) + \
    minissdpdStringEncode(st)

or

message = (struct.pack('B', 4) +
    minissdpdStringEncode(st))

I usually find the second form with parentheses easier to read.


You can continue a line by ending it with a backslash \:

message = struct.pack('B', 4) + \
    minissdpdStringEncode(st) + \
    minissdpdStringEncode(usn) + \
    minissdpdStringEncode(server) + \
    minissdpdStringEncode(location)


Add a backslash (\) at the end of each line of the statement except the last.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜