218k views
0 votes
How would you design a Python program which takes in a date from the user, a date that looks like this 3/13/17, and turns it into a date which looks like this 2017.3.13? (You don’t have to come up with every detail, just explain your approach and what keywords you would use.) I need in 2 min Plse

User Adam Vigh
by
7.9k points

1 Answer

2 votes

Answer:

To design a Python program that takes in a date in the format of "3/13/17" and converts it to the format of "2017.3.13", we can use the datetime module in Python.

Here's an approach we can take:

First, we can use the input() function to prompt the user to enter the date in the format of "m/d/yy".

Next, we can use the strptime() method of the datetime class to convert the input string to a datetime object. We can specify the input format using the %m/%d/%y format code.

We can then use the strftime() method to convert the datetime object to a string in the desired output format. We can specify the output format using the %Y.%m.%d format code.

Finally, we can print the output string to the console.

Here's some sample code that implements this approach:

from datetime import datetime

# Prompt user to enter date

date_str = input("Enter date in format m/d/yy: ")

# Convert input string to datetime object

date_obj = datetime.strptime(date_str, "%m/%d/%y")

# Convert datetime object to output string

output_str = date_obj.strftime("%Y.%m.%d")

# Print output string

print(output_str)

This program will take in a date from the user in the format of "m/d/yy", convert it to a datetime object, and then convert it to a string in the format of "YYYY.m.d". The output string will then be printed to the console.

Step-by-step explanation:

User Robin Castlin
by
8.2k points

No related questions found