1

How can I split an assignment like this onto two lines?

myLongVariableName = 'some somewhat long format string like this %s' % myOtherVariable

I tried a few variations:

myLongVariableName = 'some somewhat long format string like this %s' 
  % myOtherVariable

myLongVariableName = 'some somewhat long format string like this %s' %
  myOtherVariable

myLongVariableName = 
'some somewhat long format string like this %s' % myOtherVariable

My searching is coming up short. What I've found mostly talks about indenting blocks of code or parameters, not long expressions.

1
  • I just found that I can use a backslash at the end of the line. Is there another way? I consider lines ending with backslashes poor style. Commented Mar 13, 2015 at 19:12

2 Answers 2

2

Adjacent string literals are automatically concatenated in Python:

myLongVariableName = ('some somewhat long format'
                      'string like this %s') % myOtherVariable
Sign up to request clarification or add additional context in comments.

Comments

2

This is an option:

myLongVariableName = 'some somewhat long format string like this {}'
    .format(myOtherVariable)

Read more about it: https://docs.python.org/2/library/stdtypes.html#str.format

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.