SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
More About Strings
Exercise:
Programming Exercises
Question:3 | ISBN:9780132576376 | Edition: 2

Question

Date Printer
Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the form March 12, 2012.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Date Printer Program Code:

def formatDate(dateString):
    # Split the date string into month, day, and year
    month, day, year = date_string.split('/')

    # Create a list of month names
    month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
                   'August', 'September', 'October', 'November', 'December']

    # Convert the month string to an integer and get the corresponding month name
    month_name = month_names[int(month) - 1]

    # Print the formatted date
    print(f"{month_name} {day}, {year}")


# Get the date string from the user
dateString = input("Enter a date (mm/dd/yyyy): ")

# Format and print the date
formatDate(dateString)

Executed Output:

Enter a date (mm/dd/yyyy): 05/15/2023
May 15, 2023

 

0 0

Discussions

Post the discussion to improve the above solution.