Write a Python function that will take a string of the form “4/11/2010” and print the corresponding date in the form “Apr 11, 2010”. Make use of strings, lists, and appropriate operators/methods. In particular, define a list that contains the month abbreviations so that you can convert without needing any if-statements.

I don't really know where to start...this is all I have.

def date():
    string1=["4/11/2010"]
    list1=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
# -*- coding: utf-8 -*-

dic = { 1  : "Jan",
        2  : "Feb",
        3  : "Mar",
        4  : "Apr",
        5  : "May",
        6  : "Jun",
        7  : "Jul",
        8  : "Aug",
        9  : "Sep",
        10 : "Oct",
        11 : "Nov",
        12 : "Dec" }

def replace_date(month = 0):
    return dic[month]
    
example_date = '4/11/2010'

print replace_date(int(example_date.split('/')[0]))

Does this only print the month? All I'm getting for an output is Apr. I need it to say Apr 11, 2010. I would really appreciate the help.

# -*- coding: utf-8 -*-

dic = { 1  : "Jan",
        2  : "Feb",
        3  : "Mar",
        4  : "Apr",
        5  : "May",
        6  : "Jun",
        7  : "Jul",
        8  : "Aug",
        9  : "Sep",
        10 : "Oct",
        11 : "Nov",
        12 : "Dec" }

def replace_date( month, day, year ):
    return '%s %s, %s' % ( dic[month], day, year )
    
example_date = '4/11/2010'

print replace_date( int(example_date.split('/')[0]), example_date.split('/')[1], example_date.split('/')[2] )

Yes Kur3k soultion is ok,a little long that last print line.

dic = { 1  : "Jan",
        2  : "Feb",
        3  : "Mar",
        4  : "Apr",
        5  : "May",
        6  : "Jun",
        7  : "Jul",
        8  : "Aug",
        9  : "Sep",
        10 : "Oct",
        11 : "Nov",
        12 : "Dec" }

def replace_date(month = 0):
    return dic[month]

m, d, y = '4/11/2010'.split('/')
#print m, d, y #Test print
print '%s, %s, %s' % (replace_date(int(m)), d, y)
#-->Apr, 11, 2010

@nightrev you dident try to hard at this school task and where maybe lucky to get a finish solution for your answer.

Thank You for the help! By the way, I was just wondering how would you write this program with lists instead of dictionary?

Thank You for the help! By the way, I was just wondering how would you write this program with lists instead of dictionary?

>>> n = [i+1 for i in range(12)]
>>> m = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
>>> z = zip(n,m)
>>> z
[(1, 'Jan'),
 (2, 'Feb'),
 (3, 'Mar'),
 (4, 'Apr'),
 (5, 'May'),
 (6, 'Jun'),
 (7, 'Jul'),
 (8, 'Aug'),
 (9, 'Sep'),
 (10, 'Oct'),
 (11, 'Nov'),
 (12, 'Dec')]
>>> m, d, y = '4/11/2010'.split('/')
>>> for i in range(len(z)):
...     if int(m) in z[i]:
...         print z[i][1]
...         break
...     
Apr
>>>

I think using the 'datetime' module is the standard way. But the Locale may be a problem. Below is the code snippet showing how to:

from datetime import datetime
original = “4/11/2010”
formated = datetime.strptime(original, '%m/%d/%Y').strftime('%b %d, %Y')

Write a Python function that will take a string of the form “4/11/2010” and print the corresponding date in the form “Apr 11, 2010”. Make use of strings, lists, and appropriate operators/methods. In particular, define a list that contains the month abbreviations so that you can convert without needing any if-statements.

I don't really know where to start...this is all I have.

def date():
    string1=["4/11/2010"]
    list1=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

Yes using time or datetime is the standar way,but i am pretty sure that he can not use that in this school assignment.
Because task like this has been posted many times before,and no module could be used.

Here is another solution using only "strings, lists, and appropriate operators/methods".

def convert_mnth(date_string):
    months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
    month = months[int(date_string[-10:-8])-1]
    day = date_string[-8:-5]
    year = date_string[-5:]

    print("%s%s%s" %(month,day,year))

_____________________________________________________
>>>convert_mnth('12/23/2010')
Dec/23/2010

>>> convert_mnth('1/23/2010')
Jan/23/2010

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.