我想從 api 獲取資料,我使用這個:requests.get("https://www.metaweather.com/api/location/woeid/2013/4/i/")。我希望 woeid 是一個變數,我是一個整數。
uj5u.com熱心網友回復:
您可以使用 Python 的f-string語法將變數名替換為字串中的值。
woeid = "2487956"
i = 12
url = f"https://www.metaweather.com/api/location/{woeid}/2013/4/{i}/"
print(url)
response = requests.get(url)
網址變為:
https://www.metaweather.com/api/location/2487956/2013/4/12/
uj5u.com熱心網友回復:
CodeMonkey 答案的更長、更耗時的版本是
woeid = "2487956"
i = 12
url = "https://www.metaweather.com/api/location/" woeid "/2013/4" i "/"
print(url)
response = requests.get(url)
我添加這個的原因是,所有語言都不支持字串插值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382387.html
上一篇:PHP變數賦值沒有按預期作業
下一篇:如何在url中使用問號?
