Gmail "Flagged" status is displayed directly on the IMAP keyword \Flagged. Thus, you can switch the star of the message by setting or disabling \Flaggedin the message:
IMAP4.store(num, '+FLAGS', '\\Flagged')
You can search for marked messages by searching FLAGGED(or for unmanaged messages through UNFLAGGED):
IMAP4.search(None, 'FLAGGED')
Gmail even gives you a virtual folder containing all the marked messages. If you SELECT "[Gmail]/Starred", you will get an idea of all the marked messages in the mailbox:
IMAP4.select('[Gmail]/Starred')
dkarp source
share