datetime and the datetime.timedelta classes are your friend.
- find today.
- use that to find the first day of this month.
- use timedelta to backup a single day, to the last day of the previous month.
- print the YYYYMM string you’re looking for.
Like this:
import datetime
today = datetime.date.today()
first = today.replace(day=1)
last_month = first - datetime.timedelta(days=1)
print(last_month.strftime("%Y%m"))
201202
is printed.