0
m = raw_input("Please enter a date(format:mm/dd/yyyy): ")
def main():
    if '01' in m:
        n = m.replace('01','Janauary')
        print n
    elif '02' in m:
        n = m.replace('02','February')
        print n
    elif '03' in m:
        n = m.replace('03','March')
        print n
    elif '04' in m:
        n = m.replace('04','April')
        print n
    elif '05' in m:
        n = m.replace('05','May')
        print n
    elif '06' in m:
        n = m.replace('06','June')
        print n
    elif '07' in m:
        n = m.replace('07','July')
        print n
    elif '08' in m:
        n = m.replace('08','August')
        print n
    elif '09' in m:
        n = m.replace('09','September')
        print n
    elif '10' in m:
        n = m.replace('10','October')
        print n
    elif '11' in m:
        n = m.replace('11','November')
        print n
    elif '12' in m:
        n = m.replace('12','December')
        print n

main()

for example, this scrpt can output 01/29/1991 to January/29/1991, but I want it output to January,29,1991 How to do it? how to replace the " / " to " , "?

5
  • You use replace() method over and over in your script, maybe that should give you a good hint... Commented Nov 13, 2010 at 19:16
  • I dont know how to make it simple? Commented Nov 13, 2010 at 19:16
  • yes, but how to make the different replace() methods work together? Commented Nov 13, 2010 at 19:18
  • 3
    As a side note, your script is very badly designed; this way of doing it will not work. Try giving it "01/01/2001" as input for a hint. Commented Nov 13, 2010 at 19:21
  • I think, should think on other things, not about how to call replace twice. Your code will print "October/29/20October". It assigns n in each (el)if block, and calls print there as well... What do you want to do? Commented Nov 13, 2010 at 19:25

5 Answers 5

11

Please don't do it this way; it's already wrong, and can't be fixed without a lot of work. Use datetime.strptime() to turn it into a datetime, and then datetime.strftime() to output it in the correct format.

Sign up to request clarification or add additional context in comments.

2 Comments

@user497786: Then learn :) No one can know everything. That is why there are sites like this and documentation to read. Now you learned something new. Be happy, because it saves you a lot of work...
Ha, I'm not learn that before you tell me, I'm a new one study python.
2

Take advantage of the datetime module:

m = raw_input('Please enter a date(format:mm/dd/yyyy)')

# First convert to a datetime object
dt = datetime.strptime(m, '%m/%d/%Y')

# Then print it out how you want it
print dt.strftime('%B,%d,%Y')

Comments

1

Just like you replace all of the other strings - replace('/',',').

3 Comments

Everywhere. For example n = m.replace('11','November').replace('/',',').
@user497786 I couldn't really see making a suggestion like that, because this script is in such need of a complete rewrite anyhow...
He has the best method just above. But for a quick and dirty solution, this will work.
0

You might find a dictionary to be helpful here. It would be "simpler." You could try something as follows.

m = raw_input("Please enter a date(format:mm/dd/yyyy): ")
month_dict = {"01" : "January", "02" : "February", "03" : "March", ...}
# then when printing you could do the following
date_list = m.split("/") # This gives you a list like ["01", "21", "2010"]
print(month_dict[date_list[0]] + "," + date_list[1] + "," + date_list[2]

That will basically get you the same thing in 4 lines of code.

1 Comment

Ignacio Vazquez-Abrams has a good answer. I don't like mine anymore, but I'm leaving it here because it will get you what you're looking for as well, and could be helpful if someone is looking to do something similar without dates.
0

I have just rewrite your code more compact:

m = '01/15/2001'
d = {'01' : 'Jan', '02' : 'Feb'}

for key, value in d.items():
   if key in m:
       m = m.replace(key, value)

1 Comment

Nice solution, just wanted to type in the same code in my editor until I noticed your answer :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.