-2

I'm relatively new to python,

I built a webscraper that gets the top posts of a website and stores them in a list in python like this:

T = ["post1","post2","post3,"post4"]

To send the push notification with pushover I installed the pushover module that works like this:

from pushover import Pushover
po = Pushover("My App Token")
po.user("My User Token")
msg = po.msg("Hello, World!")
po.send(msg)

I want to send the list as a message with format, like this:

Top Posts:
1. post 1
2. post 2
3. post 3
4. post 4

I tried this:

msg = po.msg("<b>Top Posts:</b>\n\n1. "+T[0]+"\n"+"2. "+T[1]+"\n"+"3. "+T[2]+"\n"+"4. "+T[3]")

The above solution works, however the number of posts will be variable so that's not a viable solution.

What can I do to send the message with the correct formatting knowing that the number of posts in the list will vary from time to time?

topochase
  • 1
  • 1

1 Answers1

0

Using str.join and a comprehension using enumerate:

msg = po.msg("<b>Top Posts:</b>\n\n" + '\n'.join(f'{n}. s {n}' for n, s in enumerate(T, 1)))
Jab
  • 26,853
  • 21
  • 75
  • 114