michael0196
michael0196

Reputation: 1637

How do I input values from a list into a string?

I am trying to input the following list values into the url string below. When I do the following:

tickers = ['AAPL','YHOO','TSLA','NVDA']
url = 'http://www.zacks.com/stock/quote/{}'.format(tickers)`

Python returns

http://www.zacks.com/stock/quote/['AAPL', 'YHOO', 'TSLA', 'NVDA']`

What I would like it to do instead is to iterate through the list and return the following:

http://www.zacks.com/stock/quote/AAPL
http://www.zacks.com/stock/quote/YHOO
http://www.zacks.com/stock/quote/TSLA
http://www.zacks.com/stock/quote/NVDA

Thank you.

Upvotes: 5

Views: 125

Answers (6)

RoadRunner
RoadRunner

Reputation: 26315

Just iterate through tickers with a loop, and concatenate the strings together:

tickers = ['AAPL','YHOO','TSLA','NVDA']
url = 'http://www.zacks.com/stock/quote/'

for ticker in tickers:
   print(url + ticker)

# http://www.zacks.com/stock/quote/AAPL
# http://www.zacks.com/stock/quote/YHOO
# http://www.zacks.com/stock/quote/TSLA
# http://www.zacks.com/stock/quote/NVDA

Or with a list comprehension:

[url + ticker for ticker in tickers]

Which gives the combined strings in a list:

['http://www.zacks.com/stock/quote/AAPL', 
 'http://www.zacks.com/stock/quote/YHOO', 
 'http://www.zacks.com/stock/quote/TSLA', 
 'http://www.zacks.com/stock/quote/NVDA']

Upvotes: 2

Narendra
Narendra

Reputation: 1529

Try this:-

tickers = ['AAPL','YHOO','TSLA','NVDA']
url = 'http://www.zacks.com/stock/quote/'
new_ls = (x+y for x,y in zip([url]*len(tickers),tickers))
for new_url in new_ls:
    print(new_url)

Upvotes: 0

PD7
PD7

Reputation: 106

you might try this:

tickers = ['AAPL','YHOO','TSLA','NVDA']
url = 'http://www.zacks.com/stock/quote/'
for e in tickers:
    print(url + e)

this will print the urls, instead you could add them to a list aswell.

Upvotes: 1

Chuk Ultima
Chuk Ultima

Reputation: 1037

You need to join the list first before you pass it on. If you are trying to make one url with all the parameters into the url do this first :

params = "".join(tickers)
url = 'http://www.zacks.com/stock/quote/{}'.format(params)

If you want multiple url's with one parameter each time do this :

urls = []
for param in tickers:
    urls.append('http://www.zacks.com/stock/quote/{}'.format(param))

Upvotes: 2

CezarySzulc
CezarySzulc

Reputation: 2007

Use this:

tickers = ['AAPL','YHOO','TSLA','NVDA']
url = 'http://www.zacks.com/stock/quote/'
['{}{}'.format(url, x) for x in tickers]

Result is:

['http://www.zacks.com/stock/quote/AAPL',
 'http://www.zacks.com/stock/quote/YHOO',
 'http://www.zacks.com/stock/quote/TSLA',
 'http://www.zacks.com/stock/quote/NVDA']

Upvotes: 5

cs95
cs95

Reputation: 402493

A nifty trick with map:

url = 'http://www.zacks.com/stock/quote/{}'
tickers = ['AAPL','YHOO','TSLA','NVDA']

list(map(url.format, tickers))

['http://www.zacks.com/stock/quote/AAPL',
 'http://www.zacks.com/stock/quote/YHOO',
 'http://www.zacks.com/stock/quote/TSLA',
 'http://www.zacks.com/stock/quote/NVDA']

Upvotes: 7

Related Questions