Convert String to Title Case in Python

Same as .title() on the previous page, printed in one line. McDonald still becomes Mcdonald.

BeginnerString ProgramsExample 12 of 25
convert-string-to-title-case.py
1# Program to convert string to title case
2
3s = input("Enter a sentence: ")
4
5print("Title case:", s.title())

Output

Enter a sentence: python STRING programs
Title case: Python String Programs

What's going on

s.title() again. Previous program stored it in result. This one prints it immediately. Same quirk: McDonald becomes Mcdonald.

If you need real title case for names, this is the wrong function.

After this program

If the idea is still fuzzy, read Day 4: Strings and String Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 string programs, or see every topic on Python exercises.