A list of strings

list3.py

Don’t forget the commas in lines 9–22. Otherwise, you’ll have string literal concatenation without any error message.

A year has 12 months.
January is the first month.
February is the second month.
March is the third month.
April is the cruelest month.
December is the twelfth month.
December is the last month.
November is the next-to-last month.
October is the third-to-last month.

Things to try

  1. What happens if you forget one of the commas in lines 9–22?
  2. Access the individual characters of a string in the list.
    months = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    ]
    
    n = len(months)
    print(f"A year has {n} months.")
    print()
    
    print(f"{months[0]} is the first month.")
    print(f"{months[0]} has {len(months[0])} characters.")
    
    print(f"{months[0][0]} is the first character of {months[0]}.")
    print(f"{months[0][1]} is the second character of {months[0]}.")
    print(f"{months[0][2]} is the third character of {months[0]}.")
    print(f"{months[0][-1]} is the last character of {months[0]}.")
    print()
    
    print(f"{months[1]} is the second month.")
    print(f"{months[1]} has {len(months[1])} characters.")
    
    print(f"{months[1][0]} is the first character of {months[1]}.")
    print(f"{months[1][1]} is the second character of {months[1]}.")
    print(f"{months[1][2]} is the third character of {months[1]}.")
    print(f"{months[1][-1]} is the last character of {months[1]}.")
    print()
    
    print(f"{months[-1][-1]} is the last character of {months[-1]}.")
    
    A year has 12 months.
    
    January is the first month.
    January has 7 characters.
    J is the first character of January.
    a is the second character of January.
    n is the third character of January.
    y is the last character of January.
    
    February is the second month.
    February has 8 characters.
    F is the first character of February.
    e is the second character of February.
    b is the third character of February.
    y is the last character of February.
    
    r is the last character of December.
    
  3. Pass the whole list to print. The last two statements do the same thing.
    months = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    ]
    
    print(months)
    print(*months)
    print("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
    
    ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
    January February March April May June July August September October November December
    January February March April May June July August September October November December
    
  4. Put the months into one string, with one space between them.
    months = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    ]
    
    s = " ".join(months)
    print(s)
    
    January February March April May June July August September October November December
    
  5. Instead of making your own list of month names, import calendar and use the list calendar.month_name.