Code Logo

Reading Room Placard Styling

Published at22 Apr 2026
Python String Handling Medium 0 views
Like0

A library's reading room is preparing printed placards for a display table. The curator already wrote the titles, but the text came from a mix of spreadsheets, old notes, and quick copy-paste entries, so the spacing and capitalization are inconsistent.

The placard rule is more careful than plain title() formatting. First, remove outside spaces and collapse repeated internal spaces so the title reads cleanly. Then format the words so that common connector words such as and, of, the, in, on, for, to, and from stay lowercase when they appear in the middle of the title. The first and last words should still be capitalized even if they belong to that connector list.

For example, " the rise of quiet stars " should become "The Rise of Quiet Stars". A title like "notes from the harbor" should become "Notes from the Harbor". If the input is just "the", the result should be "The" because the single word is both the first and the last.

This is a formatting task with several interacting rules, so the solution has to inspect word positions as well as word values. It is not enough to capitalize everything blindly.

Example Input & Output

Example 1
Input
title = "notes from the harbor"
Output
"Notes from the Harbor"
Explanation

Connector words in the middle stay lowercase while surrounding words are capitalized.

Example 2
Input
title = "the"
Output
"The"
Explanation

A single word is both the first and the last, so it should be capitalized.

Example 3
Input
title = " the rise of quiet stars "
Output
"The Rise of Quiet Stars"
Explanation

Extra spacing is removed and the middle connector word stays lowercase.

Algorithm Flow

Recommendation Algorithm Flow for Reading Room Placard Styling
Recommendation Algorithm Flow for Reading Room Placard Styling

Solution Approach

The easiest way to reason about this problem is to treat the input as a sequence of words rather than one long string. Once the spacing is normalized, each word can be reformatted according to two pieces of information: what the word is, and where it appears in the title.

That is why a good first step is to split the text with split(). When Python uses split() without an explicit separator, repeated spaces are handled automatically, and leading or trailing whitespace disappears from the resulting word list. So the messy spacing problem becomes much smaller right away.

After that, the real logic begins. You need a set of small connector words, and then you walk through the words one by one. If a word is in the connector set and it is not the first or last word, keep it lowercase. Otherwise capitalize it. The first and last positions always win over the connector-word rule.

A clear implementation looks like this:

small = {"and", "of", "the", "in", "on", "for", "to", "from"}
words = text.split()
if not words:
    return ""

result = []
last = len(words) - 1
for i, word in enumerate(words):
    lower = word.lower()
    if i != 0 and i != last and lower in small:
        result.append(lower)
    else:
        result.append(lower.capitalize())

return " ".join(result)

The loop matters here because each word needs a positional decision. A simple one-method formatting call cannot express the full rule. That is also why this challenge is a better fit for a small controlled transformation than for a shortcut such as title(), which would incorrectly turn middle words like of and from into uppercase starters.

The resulting algorithm is still straightforward: normalize the spacing, inspect each word, apply the connector-word rule only in middle positions, and then join the final words back together. It stays readable while still handling the extra rule that makes the placard look professionally edited instead of mechanically reformatted.

Best Answers

python - Approach 1
def style_placard_title(title):
    small = {"and", "of", "the", "in", "on", "for", "to", "from"}
    words = title.split()
    if not words:
        return ""
    result = []
    last = len(words) - 1
    for i, word in enumerate(words):
        lower = word.lower()
        if i != 0 and i != last and lower in small:
            result.append(lower)
        else:
            result.append(lower.capitalize())
    return " ".join(result)