Take the 2-minute tour ×

When creating python code and keeping to the PEP8 style guide, one can have trouble to limit the line length to 79 characters when quoting a long URL in a comment:

def foo():
    # see http://stackoverflow.com/questions/13062540/replacing-text-using-regular-expression-in-python-with-named-parameters

In the actual code this looks ugly, when the URL comment overlaps with the otherwise empty indentation area on the left of the code. Is there some way to handle this in a better way, while I am still able to easily copy-and-paste the URL to put it into a web-browser?

share|improve this question
I think url shortener is good solution for such cases. You can even find auto-shorteners for you editor (e.g. there are many ready-to-use solutions for emacs). –  Alexey Kachayev Oct 25 '12 at 6:30
In the case of a StackOverflow link, SO will generate a short permalink url if you click on "share" below a question. Since the URL is less descriptive, I find it useful to add more explanation about what the link is for in the comment surrounding the link. Otherwise (for non-SO links), I think the best option is to suspend PEP8 and simply paste the long URL. You could use a URL shortener, but that 1) removes useful information and 2) introduces an additional, unnecessary single point of failure (what's to say bit.ly or any other shortener will be around in 20 years?) –  Jim Garrison Oct 25 '12 at 6:56
add comment

1 Answer

Of the many possible way, use a \ at the end of line after closing the quotes

url = "http://stackoverflow" \

response = urllib2.urlopen(url)
print response.read()

In case you have a field such as url split on multiple lines and want easier way to copy it back, use multi line string i.e. surround your whole url with 3 double quotes on both start and end. Then they can span any number of lines.

Edit: I wrote multiline comments which was suggested in comments and has been fixed

share|improve this answer
Sure, when I want to use a URL within the code. But when I have a long URL just as comment, and I want to be able to just copy and paste is very easily (without putting URL parts together), then your approach will not work. See my updated question. –  Alex Oct 25 '12 at 6:30
It wasn't clear earlier. I guess you can use multiline comments simply in that case i.e. " " " this is a multiline comment which spawns many lines like this " " "" –  fayyazkl Oct 25 '12 at 6:42
That's not a multi-line comment but a multi-line string. Big difference! –  user647772 Oct 25 '12 at 6:48
Err yeah i guess he mentioned using it in context of comments so i wrote the term in flow. Will Edit. But it does serve the purpose. –  fayyazkl Oct 25 '12 at 6:55
add comment

Your Answer


