0

I want to put the id in the link because I want to make an api call

id= 156
url1 = 'https://comtrade.un.org/api/get?r='<id>'&px=HS&ps=2020&p=0&rg=1&cc=total'

response1 = requests.get(url1)


print(response1.url)
Dario Petrillo
  • 980
  • 7
  • 15
  • You can use an f-string `f"...{id}..."` – intedgar Nov 09 '21 at 14:48
  • You can use the f"string method if its python 3 and you can use format method on a string for both versions – Maxim Kogan Nov 09 '21 at 14:50
  • Does this answer your question? [Is there a Python equivalent to Ruby's string interpolation?](https://stackoverflow.com/questions/4450592/is-there-a-python-equivalent-to-rubys-string-interpolation) – dadadima Nov 10 '21 at 12:51

2 Answers2

0

You have a lot of options here, if you want to add a lot of variables to the url or want your code to look clean, I suggest using an f-string as shown below:

url1 = f'https://comtrade.un.org/api/get?r={id}&px=HS&ps=2020&p=0&rg=1&cc=total'

This way you can put any variable in your string with just saying {variable}.

Don't forget to put the f before the quotes.

Nite Block
  • 228
  • 1
  • 3
0

Python 3 I would suggest the f"string" method as people wrote above me.

I personally like the format as it works for both 3 and 2

url1 = 'https://comtrade.un.org/api/get?r={0}&px=HS&ps=2020&p=0&rg=1&cc=total'.format(id)
Maxim Kogan
  • 135
  • 1
  • 11