HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Presenting long string literals (URLs) in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
literalslongpresentingpythonstringurls

Problem

I have a web scraping application that contains long string literals for the URLs. What would be the best way to present them (keeping in mind that I would like to adhere to PEP-8.

URL = "https://www.targetwebsite.co.foo/bar-app/abc/hello/world/AndThen?whatever=123&this=456&theother=789&youget=the_idea"
br = mechanize.Browser()
br.open(URL)


I had thought to do this:

URL_BASE = "https://www.targetwebsite.co.foo/"
URL_SUFFIX = "bar-app/abc/hello/world/AndThen"
URL_ARGUMENTS = "?whatever=123&this=456&theother=789&youget=the_idea"
br = mechanize.Browser()
br.open(URL_BASE + URL_SUFFIX + URL_ARGUMENTS)


But there are many lines and it's not a standard way of representing a URL.

Solution

You could use continuation lines with \, but it messes the indentation:

URL = 'https://www.targetwebsite.co.foo/\
bar-app/abc/hello/world/AndThen\
?whatever=123&this=456&theother=789&youget=the_idea'


Or you could use the fact that string literals next to each other are automatically concatenated, in either of these two forms:

URL = ('https://www.targetwebsite.co.foo/'
       'bar-app/abc/hello/world/AndThen'
       '?whatever=123&this=456&theother=789&youget=the_idea')

URL = 'https://www.targetwebsite.co.foo/' \
      'bar-app/abc/hello/world/AndThen' \
      '?whatever=123&this=456&theother=789&youget=the_idea'


I often use the parenthesized version, but the backslashed one probably looks cleaner.

Code Snippets

URL = 'https://www.targetwebsite.co.foo/\
bar-app/abc/hello/world/AndThen\
?whatever=123&this=456&theother=789&youget=the_idea'
URL = ('https://www.targetwebsite.co.foo/'
       'bar-app/abc/hello/world/AndThen'
       '?whatever=123&this=456&theother=789&youget=the_idea')

URL = 'https://www.targetwebsite.co.foo/' \
      'bar-app/abc/hello/world/AndThen' \
      '?whatever=123&this=456&theother=789&youget=the_idea'

Context

StackExchange Code Review Q#21658, answer score: 5

Revisions (0)

No revisions yet.