Main Content

It's the 1st, not the 1nd 1rd or 1th.

Archive - Originally posted on "The Horse's Mouth" - 2006-12-01 07:53:58 - Graham Ellis

Here's a function in Python that takes the day number in the month (in the range 1 to 31 ...) and returns "st","nd","rd" or "th" as appropriate

def somehow(val):
  "Somehow, your time will come"
  ding = (["st","nd","rd"]+["th"]*7)[(val-1) % 10]
  if val/10 == 1: ding = "th"
  return ding


You'll have noted there have been a lot of Python posts here this week .... I'm concluding my second "Programming Python" course of the week today. Next week, it's Perl ... now I wonder what will turn up on the blog!

And with the coming of December, Santa is starting to get into place. He's booked for visits to Melksham on 2nd, 9th and (I understand) the night of 24th. I snapped this festive picture the other day ... and I expect I'll be using it in the new year when I do my "ladder" example on the PHP course - perhaps Santa will end up taking over from Romeo and Juliet in my introductions!