-1

I have an HTTP request that I'm trying to conduct based on user input. To do that, I'll need to transform all non-alphanumeric characters to their corresponding percent For example, I want Broome Street to transform to Broome%20Street. Now, if spaces were all I was contending with, I would simply write a .replace(' ','%20'), but I'm quite frankly not sure what else might be in there that I haven't considered. (Maybe I'm being stupid. Either way it's an scalable question.)

Is there a simple way, perhaps a library, that will quickly and simply transform a string into a URL? The only alternative is creating two lists and zipping them through a for-loop, and that's crazy.

Yehuda
  • 1,787
  • 2
  • 15
  • 49

1 Answers1

1

There are a number of ways to do this - a few are described here: How to urlencode a querystring in Python?

Basically you're trying to perform "URL encoding". String replace and/or regex are not the best options for this, as you guessed, since there are several builtin options to take care of things for you (such as some non-obvious conversions that need to be handled)

...and don't use the first option shown on that linked page - you probably want to use

urllib.parse.quote_plus(...)
GoGoWorx
  • 181
  • 1
  • 4