How do you split long method signature to multiple lines in the most efficient way?

Experience Level: Junior
Tags: Python

Answer

To break a long line to multiple lines, use a backslash character at the end of a line. However, when you need to break a long line with strings between brackets, you can break the line without using the backslash character

Example:

def do_something():
    message = "This is" \
                       "multi-line" \
                       "message that needs backslashes" \
                       "because we are not between brackets"

def do_something_else(
    something: str,
    number: int,
    message: str):
    print(message)

# In the method above, parameters are wrapped
# to new lines without using the backslash character.
# This is possible because the parameters are between
# brackets.

Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself