3

I have the following Python code that I want to use:

initialMinute = int(input('In the initial hour of {0}:00, enter the minute of arrival'.format(initialHour))

It is a bit long, so I don't want it on one line. How can I break up the statement so that it can encompass two lines?

3 Answers 3

5

In Python, you don't actually need concatenation (the + operation) to combine strings. Literal strings on adjacent lines will be inherently concatenated:

initialHour = 4
initialMinute = int(input('In the initial hour of '
                          '{0}:00, enter the minute of arrival: '
                          .format(initialHour)))

This is actually a little more efficient than using the + operation, because the operation will be done at "compile time" rather than "runtime." Not that performance is the key issue here.

But string concatenation like this has some reasonably precise (read: nitsy and annoying) rules. It can help to know that you can use parentheses to make this kind of "implicit concatenation" more reliable. And that can help you format your code for more simplicity and readability. For instance:

initialHour = 4
question = ('In the initial hour of '
            '{0}:00, enter the minute of arrival: '
           ).format(initialHour)
initialMinute = int(input(question))

Without the parentheses, your string concatenation would likely run afoul of Python's source code indenting rules. But the parentheses signal "we're not done yet! keep waiting." Putting the closing parenthesis on the line with the format method helps connect the format method to the now-combined string.

If you're at wits end, you can also end lines with backslash characters (\) to signal "the line continues!" This can get messy and has its own set of gotchas, but can help in a pinch.


You probably don't need this quite yet, but if you can install external modules, my textdata module is aimed at solving just this type of problem. With it, instead of worrying about the details of string concatenation, you'd drop your text into a multi-line string and have the module figure out the details. E.g.:

from textdata import *

question = textline("""
    In the initial hour of
    {0}:00, enter the minute of arrival:
""")

question would then be:

'In the initial hour of {0}:00, enter the minute of arrival:'

For this one string, it's overkill. But if you include a lot of text in your programs--especially involved, indented text like HTML, XML, or SQL--it makes things easier.

Sign up to request clarification or add additional context in comments.

2 Comments

In your first example, how does .format(initialHour))) continue the line if it is not a string (that is, a string to be concatenated)?
It doesn't continue the string concatenation, per se. But the parser still has an open parenthesis it's trying to find the close for, so it is in "still looking; still adding things on" mode. If there were another string there, it'd decide to concatenate. Instead it finds a method call. But, the rules are nitsy, and require a fair amour of care. IDE's like Komodo and PyCharm that "test compile" your code while you're typing help to identify what works, what doesn't.
3

Ordinarily you can use the \ (backslash) character to indicate that your statement goes on to the next line. In your case, the long line is caused by the string, so you have to do something else.

You can separate your string using +:

initialHour = 4
initialMinute = int(input('In the initial hour of ' +
  '{0}:00, enter the minute of arrival: '.format(initialHour)))

Or you can separate the statement at parenthesis boundaries:

initialMinute = int(input(
  'In the initial hour of {0}:00, enter the minute of arrival: '.format(
    initialHour)))

Comments

2

You can separate strings onto multiple lines using the \. Use it like this:

var = "long string... \
long string..."

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.