Home > Mobile >  Python - How to print a whole url in a for loop not just characters
Python - How to print a whole url in a for loop not just characters

Time:05-16

I have a variable called all_urls- it contains a list of URLs, for example:

https://website.com/image1
https://website.com/image2
https://website.com/image3

I want to print each URL using a for loop.

for url in all_urls:
        print(url)

but what I end up getting is:

h
t
t
p
s
(and so on)

Does anyone know how to fix it so the program returns each URL? Thanks.

CodePudding user response:

You can try with this:

for url in all_urls.split('\n'):
    print(url)

What I'm thinking is that you have a string rather than a list, so when you cycle over all elements of your variable, it just prints the single character.

In this way, split('\n') will allow to generate a list by splitting on the new line, each of which element will be the single url.

CodePudding user response:

Just make a list of strings the urls and for loop

all_urls=[
    'https://website.com/image1',
    'https://website.com/image2',
    'https://website.com/image3']


for url in all_urls:
        print(url)

Output:

https://website.com/image1
https://website.com/image2
https://website.com/image3
  • Related