Skip to content
Advertisement

Python – How to print a whole url in a for loop not just characters

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement