Date Ordinal Output?

后端 未结 14 1787
囚心锁ツ
囚心锁ツ 2020-11-27 06:38

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

14条回答
  •  悲&欢浪女
    2020-11-27 06:43

    Here is an even shorter general solution:

    def foo(n):
        return str(n) + {1: 'st', 2: 'nd', 3: 'rd'}.get(4 if 10 <= n % 100 < 20 else n % 10, "th")
    

    Although the other solutions above are probably easier to understand at first glance, this works just as well while using a bit less code.

提交回复
热议问题