Home > Net >  How can I add a text in an API address with requests library (Python)
How can I add a text in an API address with requests library (Python)

Time:01-11

I want to add some texts into an API address in Python. I am using requests library and the API address is:

https://one-api.ir/translate/?token={token}&action={action}&lang={lang}&q={query}

I should insert my token in {token} and in {action} section I should put google search engine and in {lang} I should put the language that I want to translate to which is fa (persian) and in last section, {query} I should put my text that I want to translate.

this is my code:

response = requests.get('https://one-api.ir/translate/?token={token}&action={action}&lang={lang}&q={query}')

response.json()['result']

How can I get an input from user and replace them instead of {token},{action},{lang} and {query}?

CodePudding user response:

Have you looked at f-strings? f-strings are a way to format strings in python. https://docs.python.org/3/tutorial/inputoutput.html#fancier-output-formatting

Example:

token = "KeepThisSecret"
request = f"http://api.com/{token}/xyz"
print(request) # http://api.com/KeepThisSecret/xyz
  • Related