0

I am new to python so all help would be highly appreciated! I have learnt to use the enumerate method in my code, which gives a list tuples e.g. 'i like football' giving the word football position '3'. My problem is that Once I have the list and the code written out with enumerate function, I am trying to give the number given an ordinal suffix, e.g. '3' being given 'rd' and '2' being given 'nd'. Here is my code so far:

word = str(raw_input("Enter Word: "))

food = " i like computer science"
food2 = food.split()
for (num, x) in enumerate(list(food2)):
  if word == x:
    print num
T.Cho
  • 27
  • 1
  • 7
  • [this may be useful](http://stackoverflow.com/questions/3644417/python-format-datetime-with-st-nd-rd-th-english-ordinal-suffix-like) – R Nar Nov 10 '15 at 20:06
  • as it stands, i dont believe there to be anything that is built in. the datetime module can be used in a kind of hacky way by passing the index in as a date and calling `strftime` to return it in an ordinal format – R Nar Nov 10 '15 at 20:07

1 Answers1

3

You can utilize the natural package to do this (and more).

pip install natural

Then using this code, you can print out ordinals:

>>> from natural import number
>>> number.ordinal(3)
u'3rd'
>>> number.ordinal(1234567)
u'1234567th'
Andy
  • 49,085
  • 60
  • 166
  • 233