Skip to content
Programming Problems · Q2

Q.Write a user defined function to convert a string with more than one word into title case string where string is passed as parameter. (Title case means that the first letter of each word is capitalised)

Tamil Nadu DgeTextbookSubjective· 3mImportance★★★★★est
77% · 10/13 Questions
✓ Free question

Title case = capitalise the first character of every word; so split the string into words, transform each word as w[0].upper() + w[1:].lower(), and join the words back with spaces.

Idea. A string with more than one word is naturally processed word-by-word: split() gives the list of words, and for each word the title-case rule is purely positional — character 0 goes uppercase, everything after it goes lowercase (so "pYTHON" correctly becomes "Python", not "PYTHON"). join() reassembles the sentence.

def toTitleCase(sentence):
    """Return the sentence with the first letter of each word capitalised."""
    words = sentence.split()          # list of words
    newWords = []
    for w in words:
        newWords.append(w[0].upper() + w[1:].lower())
    return ' '.join(newWords)

# main program
s = input("Enter a string: ")
print("Title case string:", toTitleCase(s))

Key lines:

  • sentence.split() — splits on whitespace, e.g. "computer science" → ['computer', 'science'].
  • w[0].upper() + w[1:].lower() — slicing splits the word into its first character and the rest; upper()/lower() fix the cases; + concatenates them back.
  • ' '.join(newWords) — stitches the transformed words with single spaces.

Sample runs

Enter a string: computer science with python
Title case string: Computer Science With Python
Enter a string: aNNUAL eXaMINATION
Title case string: Annual Examination
Tip

Python has this built in: "computer science".title() → "Computer Science". Writing it yourself, as here, is what the exam expects — and the manual version behaves more predictably (title() also capitalises after apostrophes: "isn't" → "Isn'T").

Watch out

Don't forget the .lower() on w[1:]; without it, an input like "PYTHON" would stay "PYTHON" instead of becoming "Python".

✓Final answer

toTitleCase() splits the sentence, converts every word to w[0].upper() + w[1:].lower(), and joins with spaces — e.g. input "computer science with python" outputs "Computer Science With Python".

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.