3

I have the following line inside a for loop (i.e. it's indented by 4 spaces):

    abcdefgh_ijklm_nopqrstuvwxy = abcdefgh_ijklm_nopqrstuvwxy.append(abc_de)

The line is 80 characters long. How can I split it up so that it I do not get a 'Line too long' notification? Please note that I've changed the variable names for privacy reasons (not my code), but I can't modify the name of the variable, so naming it something shorter to fix the problem is not a viable option

As a secondary question, how would I split up a formatted string of the form:

data_header = f"FILE_{heading_angles}_{moment_of_inertia}_{mass_of_object}_{type_of_object}"

to span multiple lines? I already tried

data_header = f"FILE_{heading_angles}_{moment_of_inertia}_"
              f"{mass_of_object}_{type_of_object}"

but that gives me an indentation error.

Any kind of help would be greatly appreciated!

Kadhir
  • 143
  • 1
  • 2
  • 12

2 Answers2

3

Hope that these points answer your questions:

  1. To simplify your expressions, try to replace the variables with simpler ones before the expressions. This may be inappropriate, if more serious operations are needed. For example:
a = abcdefgh_ijklm_nopqrstuvwxy
b = abcdefgh_ijklm_nopqrstuvwxy.append(abc_de)
a = b
  1. In your case, try using a forward-leaning backlash (\) at the end of the line. For example:
if a == True and \
   b == False

Here is a link from another discussion on a similar matter.

Hope this helps.

Run_Script
  • 2,487
  • 2
  • 15
  • 30
0

For you data_header example - you need brackets.

For example:

data_header = (
    f"FILE_{heading_angles}_{moment_of_inertia}_"
    f"{mass_of_object}_{type_of_object}"
)
ostergaard
  • 3,377
  • 2
  • 30
  • 40