Emin Mastizada
Emin Mastizada

Reputation: 1405

Using a lot of %s in program, how to put variables at the end in multiline?

For example:

command """%s abc %s,
        abs %s jfh %s etc...""" % (var1, var2, var3, var4)

I need to write that vars in multiline because there are a lot of them in code (for updating database).

Tested:

% (var1, var2,\
var3,var4)

not working. (Also tested without "," at the end of first line)

Error: Showing last ")" and:

SyntaxError: EOL while scanning string literal

PS. We got a good answer to this question, also problem was "%s"""" at the end, it should be: "%s" """ - yes, one blank space solves problem)))

Upvotes: 1

Views: 109

Answers (1)

Hooked
Hooked

Reputation: 88218

Ignoring the valid SQL concerns, a better way to handle a multiline string problem is with str.format. This way you can store all your "data" in a dictionary and format at the very end. This will avoid (var1, var2, ..., varN). For example:

data = {'name':'Hooked',
        'site':'Stack Overflow',
        'adj' :'awesome'}

s = "{name} thinks {site} is {adj}!"

print s.format(**data)

#>> Hooked thinks Stack Overflow is awesome!

Upvotes: 8

Related Questions