Editor

Title Case With Small Words

#256 · Strings · Beginner · 40 XP · about 8 min

Book and film titles are written in title case, but short words like "of" and "the" usually stay lowercase. Write title_case(title) that formats a title with these rules:

  • Every word is written in lowercase with a capital first letter.
  • The small words a, an, the, of, and, in and on stay completely lowercase,
  • unless the small word is the first or the last word of the title: those always get a capital.

The words in title are separated by single spaces and can be in any mix of upper and lower case.

Examples

title_case("the lord of the rings")  # returns 'The Lord of the Rings'
title_case("a tale OF two cities")   # returns 'A Tale of Two Cities'
title_case("the world we live in")   # returns 'The World We Live In'

In the last example "in" is the last word, so it gets a capital.

Note

title has at least one word, and the words contain only letters.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.