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.
精彩评论