Search code examples
pythonperformancetwiliomms

Is there a way to clean up this code to make it more efficient and not look so messy?


I have 2 .jpg pictures that I am sending. They both are called as follows: 'wow1', 'wow2'. The code below works when I send it, but it doesn't look very pretty. How can I clean this up?

for n in range (1,3):
    address = 'http://exampleaddress.com/rowdycode/wow'
    extension = '.jpg'
    picture =str(n)
    p = str(address+picture+extension)
    media_url = p

If I give it a print function, it prints as follows:

http://exampleaddress.com/rowdycode/wow1.jpg
http://exampleaddress.com/rowdycode/wow2.jpg

Thank you in advance.


Solution

  • From python 3.6, you can also use Literal String Interpolation(f-strings)

    address = [f'http://exampleaddress.com/rowdycode/wow{n}.jpg' for n in range(1,3)]