Printing a calendar for a month based on user input I have a long piece of code that has many repeating points. I am sure that it doesn't take this amount of code to create a simple calendar, but I do not know how to shorten this piece of code.

# Creating a calendar in proportion to the user defined starting date

n = int(input('Enter number of days: '))#days in a month
s = input('The first day of the week: ')#The date that user wants the month to start
s = s.capitalize()

lis1 = ['Sa','M','T','W','Th','F','Su']
print('{:&gt;3}{:&gt;4}{:&gt;4}{:&gt;4}{:&gt;4}{:&gt;4}{:&gt;4}'.format('Sa','M','T','W','Th','F','Su'))

for i in range(1,n+1):#loop to generate the dates for a month
  if s==lis1[0] and i ==1:#this is to determine how much should the code indent to align to traditional calendar formatting
    print('{:&gt;3}'.format(i),end =' ')
  elif s==lis1[1] and i ==1:
    print(' '*4+'{:&gt;3}'.format(i),end =' ')
  elif s==lis1[2] and i ==1:
    print(' '*8+'{:&gt;3}'.format(i),end =' ')
  elif s==lis1[3] and i ==1:
    print(' '*12+'{:&gt;3}'.format(i),end =' ')
  elif s==lis1[4] and i ==1:
    print(' '*16+'{:&gt;3}'.format(i),end =' ')
  elif s==lis1[5] and i ==1:
    print(' '*20+'{:&gt;3}'.format(i),end =' ')
  elif s==lis1[6] and i ==1:
    print(' '*24+'{:&gt;3}'.format(i),end =' ')
  else:
    print('{:&gt;3}'.format(i),end =' ')#after the indent this is to print the remaining dates

  if s==lis1[0] and i%7==0:#this is to print a new line in proportion the choosen starting date so as to stay in check with the traditional calendar format
    print()
  elif s==lis1[1] and i%7==6:
    print()
  elif s==lis1[2] and i%7==5:
    print()
  elif s==lis1[3] and i%7==4:
    print()
  elif s==lis1[4] and i%7==3:
    print()
  elif s==lis1[5] and i%7==2:
    print()
  elif s==lis1[6] and i%7==1:
    print()


This is an example of the calendar I am outputting.



Please guide me on shortening code that is related to loops if possible.
First improvement


  don't repeat yourself,  the dry principle


You have many elif statements that can be easily rewritten using a dictionary. 

My final program has none at all. I rewrote your lst (also badly named) into a dictionary like this:

weekdays = {'Su':0,'M':1,'T':2,'W':3,'Th':4,'F':5,'Sa':6}


Now weekdays is used on 2 different locations to replace the long elif structure.

First elif block. Notice they all increment by 4. So I can use the value of the dictionary like this: weekdays[first_weekday]*4

Second elif block. Here you can use the same dictionary. Notice how weekdays[first_weekday] + current_day becomes a multitude of 7.

Second Improvement

You should rename the variables in your program to understand what they do. For example n should be renamed to number_of_days

A good way to follow are the PEP8 guidelines

Final Program

As you can see, this program not only is shorter but also far more readable

# I omitted comments because the names speak for themselves
number_of_days = int(input('Enter number of days in month: '))
first_weekday = input('First weekday of the month: ')

# Using a dictionary over a list is the biggest improvement in my opinion
weekdays = {'Su': 0, 'M': 1, 'T': 2, 'W': 3, 'Th': 4, 'F': 5, 'Sa': 6}
print('{:&gt;3}{:&gt;4}{:&gt;4}{:&gt;4}{:&gt;4}{:&gt;4}{:&gt;4}'
      .format('Su', 'M', 'T', 'W', 'Th', 'F', 'Sa'))

# First elif block
print (weekdays[first_weekday]*4*' ' + '{:&gt;3}'.format(1), end=' ')
for current_day in range(1, number_of_days+1):
    # Second elif block
    if (weekdays[first_weekday] + current_day) % 7 == 0:
        print ()
    print ('{:&gt;3}'.format(current_day ), end=' ')