Python provides the built-in title() string method to convert a string into title case, where the first character of each word is capitalized and all remaining characters are converted to lowercase. This method is commonly used for formatting headings, names and titles in text processing and data cleaning tasks.
The title() method does not modify the original string and always returns a new formatted string.
Let's understand with the help of an example:
a = "hello geek"
b = a.title()
print(b)
Output
Hello Geek
Syntax of String title()
string.title()
Parameters
title() doesn't accept any parameter.
Return type
string, converted to title case.
Creating Proper Names from User Input
When we ask users to input their names, we often receive names in lowercase or inconsistent formats. The title() method can help ensure that each part of the name starts with a capital letter.
user_name = "geeky geek"
formatted_name = user_name.title()
print(formatted_name)
Output
Geeky Geek
Cleaning Data for Database
When importing or processing data for a database, we may encounter strings that need to be formatted in title case. This ensures that data is consistent and easily readable when displayed in a user interface.
city_name = "noida city"
formatted_city_name = city_name.title()
print(formatted_city_name)
Output
Noida City