Introduction
In this lab, you will learn how to check if a string is in title case in Python. The lab begins by explaining the concept of title case, a capitalization style commonly used in titles and headings, where the first letter of each word is capitalized, except for minor words like articles, prepositions, and conjunctions.
You will then create a Python script named title_case.py that includes a function to convert a given string to title case. The script splits the string into words, capitalizes the first letter of each word (excluding minor words), and joins the words back together. Finally, you will run the script and observe the output, demonstrating the conversion of strings to title case.
Understand Title Case
In this step, you will learn about title case, a common capitalization style used in titles and headings. Understanding title case is essential for formatting text correctly and ensuring readability.
Title case refers to a style where the first letter of each word is capitalized, except for certain minor words such as articles (a, an, the), prepositions (of, in, to), and conjunctions (and, but, or). However, the first and last words of the title are always capitalized, regardless of their type.
Let's start by creating a Python script to explore title case.
Open the VS Code editor in the LabEx environment.
Create a new file named
title_case.pyin the~/projectdirectory.touch ~/project/title_case.pyOpen the
title_case.pyfile in the editor.Add the following Python code to the file:
def to_title_case(text): minor_words = ['a', 'an', 'the', 'of', 'in', 'to', 'and', 'but', 'or'] words = text.split() title_cased_words = [] for i, word in enumerate(words): if i == 0 or i == len(words) - 1 or word not in minor_words: title_cased_words.append(word.capitalize()) else: title_cased_words.append(word.lower()) return ' '.join(title_cased_words) text1 = "the quick brown fox" text2 = "learning python is fun" print(to_title_case(text1)) print(to_title_case(text2))This code defines a function
to_title_casethat converts a given string to title case. It splits the string into words, capitalizes the first letter of each word (except for minor words), and then joins the words back together.Save the
title_case.pyfile.Run the script using the
pythoncommand in the terminal:python ~/project/title_case.pyYou should see the following output:
The Quick Brown Fox Learning Python Is FunThis output demonstrates how the script converts the input strings to title case, capitalizing the first letter of each significant word.
Use istitle() Method
In this step, you will learn how to use the istitle() method in Python to check if a string is in title case. The istitle() method is a built-in string method that returns True if the string is title-cased and False otherwise.
To understand how istitle() works, let's modify the title_case.py script from the previous step.
Open the
title_case.pyfile in the VS Code editor.Add the following code to the file:
def to_title_case(text): minor_words = ['a', 'an', 'the', 'of', 'in', 'to', 'and', 'but', 'or'] words = text.split() title_cased_words = [] for i, word in enumerate(words): if i == 0 or i == len(words) - 1 or word not in minor_words: title_cased_words.append(word.capitalize()) else: title_cased_words.append(word.lower()) return ' '.join(title_cased_words) text1 = "the quick brown fox" text2 = "Learning Python is fun" text3 = to_title_case(text1) print(text1.istitle()) print(text2.istitle()) print(text3.istitle())In this code, we are using the
istitle()method to check iftext1,text2, andtext3are in title case.text1is in lower case,text2is partially in title case, andtext3is the result of convertingtext1to title case using theto_title_casefunction from the previous step.Save the
title_case.pyfile.Run the script using the
pythoncommand in the terminal:python ~/project/title_case.pyYou should see the following output:
False False TrueThis output shows that
text1andtext2are not in title case (soistitle()returnsFalse), whiletext3is in title case (soistitle()returnsTrue).
Check for Proper Capitalization
In this step, you will learn how to combine the to_title_case function and the istitle() method to check if a given string is properly capitalized according to title case rules. This involves converting the string to title case and then verifying if the converted string is indeed in title case.
Let's continue modifying the title_case.py script from the previous steps.
Open the
title_case.pyfile in the VS Code editor.Add the following code to the file:
def to_title_case(text): minor_words = ['a', 'an', 'the', 'of', 'in', 'to', 'and', 'but', 'or'] words = text.split() title_cased_words = [] for i, word in enumerate(words): if i == 0 or i == len(words) - 1 or word not in minor_words: title_cased_words.append(word.capitalize()) else: title_cased_words.append(word.lower()) return ' '.join(title_cased_words) def check_title_case(text): title_cased_text = to_title_case(text) return title_cased_text.istitle() text1 = "the quick brown fox" text2 = "Learning Python is fun" text3 = "The Quick Brown Fox" print(check_title_case(text1)) print(check_title_case(text2)) print(check_title_case(text3))In this code, we define a new function
check_title_casethat takes a string as input, converts it to title case using theto_title_casefunction, and then uses theistitle()method to check if the converted string is in title case. We then test this function with three different strings:text1,text2, andtext3.Save the
title_case.pyfile.Run the script using the
pythoncommand in the terminal:python ~/project/title_case.pyYou should see the following output:
True True TrueThis output shows that
text1andtext2are converted to title case and theistitle()method returnsTrue.text3is already in title case, and thecheck_title_casefunction confirms this.
Summary
In this lab, you begin by understanding the concept of title case, a capitalization style where the first letter of each word is capitalized, except for minor words like articles, prepositions, and conjunctions (with the first and last words always capitalized).
You then create a Python script named title_case.py that defines a function to_title_case to convert a given string to title case. The script splits the string into words, capitalizes the appropriate words, and joins them back together. Finally, you run the script to observe the output, demonstrating the conversion of sample strings to title case.



