Open In App

Python String Title method

Python String title() method returns a new string after converting the first letter of every word to uppercase(capital) and keeping the rest of the letters lowercase.

Example: Make string title case






print("usE stRing title Method".title())

Output

Use String Title Method

String title() Method Syntax

string.title()



Parameters

Return

string, converted to title case.

What is String title() Method?

String title() is an in-built function in Python that is used to change text in title format. It converts the first letter of every word to uppercase and other letters to lowercase and then returns this new string.

It is very useful function for string formatting. You can easily format texts for page titles or headings.

Note: Any non-alphabetic character is considered as the start of a new word in the title function, for example it will turn “do’s” to “Do’S”.

How to Use String title() Function

String title() function is a very easy string method. You just need to call the function with object string. Let’s look at how to convert a string to title with an example:




# using string title function
print("cOnverting to TiTle".title())

Output

Converting To Title

More Example on String title() Method

Here are some more examples on string title() method, for better understanding:

Example 1: Convert string to title




print("geeks_for_geeks".title())

Output:

Geeks_For_Geeks

Example 2: Basic usages of String title() Method




# conversion from mixed case
str1 = 'geeKs foR geEks'
print(str1, 'converted to using title():', str1.title())
 
# conversion from all lower case
str4 = 'geeks for geeks'.title()
print(str4, 'converted to using title():', str4.title())
 
# conversion from all UPPER CASE
str5 = 'WE ARE 1'.title()
print(str5, 'converted to using title():', str5.title())

Output: 

geeKs foR geEks converted to using title(): Geeks For Geeks
Geeks For Geeks converted to using title(): Geeks For Geeks
We Are 1 converted to using title(): We Are 1

Limitations of String title() Method

There are some limitations of string title() function. We have mentioned some of the limitations that you might notice:

Example 1: title() method considers any non-alphabet as a word boundary.




string = "He's smarter."
expected_string = "He's Smarter"
print("Expected:", expected_string, ", Actual:", string.title())

Output:

Expected: He's Smarter , Actual: He'S Smarter.

Explanation: The ‘s’ after He’ is converted to Capital letter, because after (apostrophe) string title() method considers the start of a new word, since s is the first letter after (apostrophe), thus it makes ‘s’ as capital letter.

Example 2: Different word boundaries other than space, when using title() Method

Here, even though there are no spaces separated words, still Python String title() method converts the String title case considering ‘-‘(hyphen) as word boundaries.




string = "geeks-for-geeks"
print(string, "converted using title():", string.title())

Output:

geeks-for-geeks converted using title(): Geeks-For-Geeks

Fixing the Limitations of title() Method

Now let’s look at the technique that you can use and avoid this unexpected behaviour of string title() function.

Using Regex to fix the unexpected behavior of String title() Method.




import re
 
 
def to_title(string):
    regex = re.compile("[a-z]+('[a-z]+)?", re.I)
    return regex.sub(lambda grp: grp.group(0)[0].upper() + grp.group(0)[1:].lower(),
                     string)
 
 
print(to_title("I won't be working tomorrow."))

Output:

I Won't Be Working Tomorrow.

In this article we have covered the definition, syntax and uses of string title() method in Python. We also covered the limitation of this function and how to work around it.

String title() function is very useful string method for text formatting.

Read More Python String Methods

Similar Reads:

FAQ on Python title() method

Q: What does the title() method do in Python?

The title() method is a built-in Python string method that returns a new string with the first character of each word capitalized, and all other characters in lowercase. It is used to convert a string into title case format.

Q: How does the title() method work?

The title() method scans the string and identifies word boundaries based on whitespace characters. It then converts the first character of each word to uppercase and the remaining characters to lowercase. It returns a new string with the modified formatting.

Q: What is the syntax for using the title() method?

The title() method is called on a string object using dot notation. The syntax is as follows: string.title()

Q: Does the title() method modify the original string?

No, the title() method does not modify the original string. Instead, it returns a new string with the modified formatting. If you want to store the modified string, you need to assign the result to a variable.


Article Tags :