Friday, November 15, 2024
Google search engine
HomeLanguagesPython String Title method

Python String Title method

The String title() method in Python is used to convert the first character in each word to uppercase and the remaining characters to lowercase in the string and returns a new string.

Python String title() Method Syntax:

Syntax: str.title()

parameters: title() doesn’t accept any parameter.

Return: str, converted to title case

Python String title() Method Example:

Python3




print("Lazyroar_for_Lazyroar".title())


Output:

Geeks_For_Geeks

Example 1: Basic usages of Python String title() Method

Python




# conversion from mixed case
str1 = 'geeKs foR geEks'
print(str1, 'converted to using title():', str1.title())
 
# conversion from all lower case
str4 = 'Lazyroar for Lazyroar'.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

Getting undesired results while using Python String title() Method

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

Python3




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.

Python3




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


Output:

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

Word around to unexpected behavior of String title() Method

Example 1: Using Regex to fix the unexpected behavior of Python String title() Method.

Python3




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.

FAQ on Python title() method

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

A: 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?

A: 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?

A: 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?

A: 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.

RELATED ARTICLES

Most Popular

Recent Comments