Wrapping long code on a new line in Python

Contents
Intro
Example
Wrapping f-string
Assignment
Function
if
List
Wrapping import
Related Articles

Introductio

When line exceeds 80 symbols according to PEP 8 it should be wrapped.

Example

Example of a string that is too long

url = your_base_url + "/monitor-service/api/v1/components/744618a0-78c5-4e19-78f4-6d215bde64a5"

To make a line break, use the \ symbol

url = your_base_url + \ "/monitor-service/api/v1/components/744618a0-78c5-4e19-78f4-6d215bde64a5"

Or

url = your_base_url + "/monitor-service/api/v1/components/" \ "744618a0-78c5-4e19-78f4-6d215bde64a5"

f-string

When wrapping f-string , for example:

print(f'\n\nPOST to {your_url} response status code is {response.status_code}\n')

New line should also starts with f

print(f'\n\nPOST to {your_url} response status code is ' f'{response.status_code}\n')

If after the wrapping some line doesn't have variable placeholders - you don't need to write f

url: str = "https://aredel.com" print("Website about software testing:" f"{url} has https," "can be found at link.")

If a long f-string is used when declaring a variable, it must be enclosed in brackets for wrapping

current_time = time.strftime("%Y-%m-%d-%H-%M-%S") epoch_time = int(time.time()) url = "https://testsetup.ru" message = (f"В данный момент: {current_time}" f"linux epoch time: {epoch_time}" f"{url} содержит https, переходите по ссылке")

Assignment

When wrapping expressiong like a = b, where b is being too long:

# Good: # Wrapping by opening bracket. foo = long_function_name(var_one, var_two, var_three, var_four) # Alternative is a "Handing" indent. foo = long_function_name( var_one, var_two, var_three, var_four) # If you put a comma at the end, the closing bracket # can be placed under the first non-empty character. result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', ) # Or to the beginning of the line. result = some_function_that_takes_arguments( 'a', 'b', 'c', 'd', 'e', 'f', ) # Wrong: # It is forbidden to list arguments on the first line # if the next line is not aligned. foo = long_function_name(var_one, var_two, var_three, var_four)

Function Declaration

If you need to declare a function with a large number of parameters:

# Right: # You need to indent 4 spaces to highlight the parameters. def long_function_name( var_one, var_two, var_three, var_four): print(var_one) # Wrong # Parameters are not highlighted and difficult to read def long_function_name( var_one, var_two, var_three, var_four): print(var_one)

if

Branching based on if is allowed in the following ways::

# No indentation. if (this_is_one_thing and that_is_another_thing): do_something() # A good trick is to add a comment, which will # improve readability in editors with syntax highlighting. if (this_is_one_thing and that_is_another_thing): # Since both conditions are true, we can frobnicate. do_something() # Allowed to add indentation before and. if (this_is_one_thing and that_is_another_thing): do_something()

Declaration of lists

Lists can be declared in two ways:

my_list = [ 1, 2, 3, 4, 5, 6, ] my_list = [ 1, 2, 3, 4, 5, 6, ]

Wrapping import

If you need to do a transfer when importing modules, you need to enclose the module names in brackets

from devhopspac import (first_long_function_name, second_long_function_name)

Article author: Andrei Olegovich

Related articles
Interactive mode
str: strings
\: long line wrapping
Lists []
if, elif, else
Loops
Methods
Functions
*args **kwargs
enum
Get Variable Type
Testing with Python
Calling REST API with Python
Files: write, read, append, context manager…
Download File
SQLite3: database
datetime: Date and Time в Python
json.dumps
Selenium + Python
Issues with Python
DJANGO
Flask
ZPL printer script
socket :Python Sockets
Virtual Environment
subprocess: shell commands execution
multiprocessing
psutil: system usage
sys.argv: command prompt variables
PyCharm: IDE
pydantic: data validation
paramiko: SSH with Python
enumerate
f-string

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: