13

if i have a very long line of a code, is it possible to continue it on the next line for example:

 url='http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|'
+ '100,000|1,000,000&chxp=1,0&chxr=0,0,' +
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:'
codeforester
  • 39,467
  • 16
  • 112
  • 140
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

4 Answers4

21

I would write it like this

url=('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|'
     '100,000|1,000,000&chxp=1,0&chxr=0,0,%(max_freq)s300|1,0,3&chxs=0,676767'
     ',13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465'
     '&cht=bvs&chco=A2C180&chds=0,300&chd=t:'%{'max_freq': max(freq)})

Note that the + are not required to join the strings. It is better this way because the strings are joined at compile time instead of runtime.

I've also embedded %(max_freq)s in your string, this is substituted in from the dict at the end

Also check out urllib.urlencode() if you want to make your url handling simpler

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
17

Where to look for help in future

Most syntax problems like this are dealt with in PEP 8. For the answer to this question, you can refer to the section "Code Layout".

Preferred way : Use (), {} & []

From PEP-8:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression...

This means your example would like like this:

url= ('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' +
      '100,000|1,000,000&chxp=1,0&chxr=0,0,' +
      max(freq) + 
      '300|1,0,3&...chco=A2C180&chds=0,300&chd=t:')

The alternate way: Use \

From PEP-8:

...but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.

url = 'http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' + \
      '100,000|1,000,000&chxp=1,0&chxr=0,0,' + \ 
       max(freq) + \
      '300|1,0,3&...chco=A2C180&chds=0,300&chd=t:'

Avoiding concatenation

String formatting

In this case, we only have a single thing that we would like to be changed in the URL: max(freq). In order to efficiently insert this into a new string, we can use the format method with numerical or named arguments:

url = "http://...{0}.../".format(max(freq))
url = "http://...{max_freq}.../".format(max_freq=max(freq))
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
  • 1
    Do you need to use `+ \ ` or can you just use `\ `? – Nick T May 13 '14 at 22:07
  • PEP 8 says that the recommended style is to break before binary operators. So shouldn't the `+` be at the beginning of each line break in the fist way. – Johann Bzh Mar 09 '21 at 09:51
2

Python combines two strings literal together, so

>>> s = "abc" "def"
>>> s
'abcdef'

but that wouldn't work if they are on two lines because Python doesn't know that the next line is part of the command. To solve that you can use backslash or brackets.

>>> s = ("hello, world"
"!")
>>> s
'hello, world!'

and you wouldn't need + to attach the strings together. You will still need it for adding nonliterals like max(freq), as explained in String Literal Concatenation. This is marginally more efficient, but more importantly clearer and enables commenting parts of a string as shown in the Python documentation linked.

Muhammad Alkarouri
  • 23,884
  • 19
  • 66
  • 101
1

Yes, use a backslash \, like so:

url='http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' + \
'100,000|1,000,000&chxp=1,0&chxr=0,0,' + \ 
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:' 

Or you can wrap your expression with parentheses ():

url=('http://chart.apis.google.com/chart?chxl=1:|0|10|100|1,000|10,000|' +
'100,000|1,000,000&chxp=1,0&chxr=0,0,' +  
      max(freq) + '300|1,0,3&chxs=0,676767,13.5,0,l,676767|1,676767,13.5,0,l,676767&chxt=y,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,300&chd=t:') 
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • 2
    Note that the `+` is not needed if you're simply appending strings. Something like `'foo' + 'bar'` is equivalent to `'foo' 'bar'` – Wolph Aug 03 '10 at 23:34
  • 4
    -1 Backslashes are ugly and evil. If you accidentally have a space after the backslash, you get `SyntaxError: unexpected character after line continuation character`. Plus is evil -- it is evaluated at runtime. – John Machin Aug 04 '10 at 01:04