Mez Mez - 6 months ago 27
Python Question

Date Ordinal Output?

I'm wondering if there is a quick and easy way to output ordinals given a number in python.

For example, given the number

1
, I'd like to output
"1st"
, the number
2
,
"2nd"
, et cetera, et cetera.

This is for working with dates in a breadcrumb trail

Home > Venues > Bar Academy > 2009 > April > 01


is what is currently shown

I'd like to have something along the lines of

Home > Venues > Bar Academy > 2009 > April > 1st

Answer

Or shorten David's answer with:

if 4 <= day <= 20 or 24 <= day <= 30:
    suffix = "th"
else:
    suffix = ["st", "nd", "rd"][day % 10 - 1]