[Tutor] simplifying with string-formatting operator

Pujo Aji ajikoe at gmail.com
Fri Sep 23 18:28:25 CEST 2005


hello,
 you can use this model:

day, month, year = input("Please enter the day, month and year numbers: ")
date1 = '%s/%s/%s' % (month, day, year)

months = {1:"January", 2:"February", 3:"March", 4:"April",
5: "May", 6: "June", 7: "July", 8: "August",
9: "September", 10: "October", 11: "November", 12: "December"}

date2 = '%s %s, %s' % (months[int(day)], day, year)
print 'the date is %s or %s' % (date1, date2)
 cheers,
pujo

 On 9/23/05, Goofball223 at wmconnect.com <Goofball223 at wmconnect.com> wrote:
>
> Hello
>
> Does anyone have any idea on how i could simplify the following program by
> using strings?
>
> # dateconvert2.py
> # Converts day month and year numbers into two date formats
>
> import string
>
> def main():
> # get the day month and year
> day, month, year = input("Please enter the day, month and year numbers: ")
>
>
> date1 = str(month)+"/"+str(day)+"/"+str(year)
>
> months = ["January", "February", "March", "April",
> "May", "June", "July", "August",
> "September", "October", "November", "December"]
> monthStr = months[month-1]
> date2 = monthStr+" " + str(day) + ", " + str(year)
>
> print "The date is", date1, "or", date2
>
> main()
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050923/9acdc7e5/attachment.htm


More information about the Tutor mailing list