Home > Back-end >  How do I create a dynamic url based on user input using python?
How do I create a dynamic url based on user input using python?

Time:03-30

New to python here so this is mostly a syntax & library question. I'm looking for the most efficient way to create a dynamic url within python that is built on user input to later search and parse.

Example of the code I have so far:

collection = "MERRA"
version = "5.12.4"

url = "https://misc.gov/search/granules.umm_json?short_name=" , {collection}, "&" , "Version=" ,{version}, "&pretty=true'"

Output:

>>> print(url)
('https://misc.gov/search/granules.umm_json?short_name=', {'M2I1NXASM'}, '&', 'Version=', {'5.12.4'}, "&pretty=true'")

Goal Output:

>>> print(url)
https://misc.gov/search/granules.umm_json?short_name=M2I1NXASM&Version=5.12.4&pretty=true

collection and version are manually defined by the user for now.

Which python libraries (if needed) are best to use for this? How can I fix my syntax so that my output doesn't contain spaces, quotes, or curly brackets?

CodePudding user response:

Try this method to string concatenation with variable value substitution:

collection = "MERRA"
version = "5.12.4"
url = "https://misc.gov/search/granules.umm_json?short_name=" str(collection) "&" "Version=" str(version) "&pretty=true"
print('#'*100)
print(url)

####################################################################################################
https://misc.gov/search/granules.umm_json?short_name=MERRA&Version=5.12.4&pretty=true
  • Related