Send a formated list with pushover on python

156 Views Asked by At

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?

1

There are 1 best solutions below

2
Jab On

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)))