Использование значений в списке для замены подстроки в отдельной переменной

#python #list #loops

#python #Список #циклы

Вопрос:

Я новичок в python и пытаюсь создать функцию, которая заменит подстроку в одной переменной значениями в списке в другой переменной.

 listofstocks = ["NET","DAL","DG","KNDI"]
apiurl = 'https://cloud.iexapis.com/stable/stock/{}/advanced-stats'
  

Я не уверен, как написать цикл, который делает это, но мой ожидаемый результат должен быть

 https://cloud.iexapis.com/stable/stock/NET/advanced-stats
https://cloud.iexapis.com/stable/stock/DAL/advanced-stats
https://cloud.iexapis.com/stable/stock/DG/advanced-stats
https://cloud.iexapis.com/stable/stock/KNDI/advanced-stats
  

Ответ №1:

 for stock in listofstocks:
    apiurl.format(stock)
  

Ответ №2:

Спасибо!

Ответ был:

 listofstocks = ["NET","DAL","DG","KNDI"]
apiurl = 'https://cloud.iexapis.com/stable/stock/{}/advanced-stats'

for stock in listofstocks:
    hello = apiurl.format(stock)
    print(hello)
  

Ответ №3:

сделайте это (не с переменной):

 listofstocks = ["NET","DAL","DG","KNDI"]
apiurl = 'https://cloud.iexapis.com/stable/stock/{}/advanced-stats'
for stock in listofstocks:
    print(apiurl.format(stock))
  

(с переменной)

 listofstocks = ["NET","DAL","DG","KNDI"]
apiurl = 'https://cloud.iexapis.com/stable/stock/{}/advanced-stats'
for stock in listofstocks:
    fstr = apiurl.format(stock)
    print(fstr)