Chris
Chris

Reputation: 40623

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

I'm lost in Python world:

message = 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.

Upvotes: 3

Views: 455

Answers (3)

kindall
kindall

Reputation: 184101

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

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 992887

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.

Upvotes: 8

samplebias
samplebias

Reputation: 37899

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

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

Upvotes: 2

Related Questions