Online Tutorials & Training Materials | STechies.com
Register Login

Python String Title() Method

04 Oct 2019 9:23 am || 0

What is String Title() Method?

While programming, you might have to deal with strings that have different kinds of characters. Strings might have special characters, numbers along with lower and uppercase characters. It is not possible to change the case of the characters manually. Python provides a facility for converting the case of the first characters of strings.

In python title() function is used to convert the first character of any word present in the string in uppercase and convert rest of the character in lower case and returns a new string.

Syntax

str.title()

Parameters

  • No parameters

Return Value

  • Returns string with the first character of every word in uppercase and remaining characters in lower case.

Example 1

Python Code To Explain title() Method

# Python code to explain title() Method 
# Initialize a string with all lowercase characters
str1 = 'python code to explain title method'
# Initialize a string with a first uppercase character
str2 = 'Python Code to Explain Title Method'
# Initialize a string with all uppercase character
str3 = 'PYTHON CODE TO EXPLAIN TITLE METHOD'
# Initialize a string with a first numeric character
str4 = '1python 2code to 2explain title method'
# Pass strings with title() method
TitleStr1 = str1.title()
TitleStr2 = str2.title()
TitleStr3 = str3.title()
TitleStr4 = str4.title()
# Print output
print('String 1: ', str1, ' New 1: ',TitleStr1)
print('String 2: ', str2, ' New 2: ',TitleStr2)
print('String 3: ', str3, ' New 3: ',TitleStr3)
print('String 4: ', str4, ' New 4: ',TitleStr4)

Output

String 1:  python code to explain title method  New 1:  Python Code To Explain Title Method

String 2:  Python Code to Explain Title Method  New 2:  Python Code To Explain Title Method

String 3:  PYTHON CODE TO EXPLAIN TITLE METHOD  New 3:  Python Code To Explain Title Method

String 4:  1python 2code to 2explain title method  New 4:  1Python 2Code To 2Explain Title Method

Note: If The first character of the word in the input string is numeric then it will convert the second character to Uppercase.

Explanation

In the above code, we have initialized 4 different strings with 4 different values.

Then the title() method is applied to the string variables such as str1.title() and the value is stored in a variable called TitleStr1. The same is done for the other strings str2, str3 and str4 and assigned in variables TitleStr2, TitleStr3 and TitleStr4. And are printed on the screen using print() method.

Variables str1, str2 and str3 are then converted into 'Python Code to Explain Title Method' by the title() method where all the first characters of every word are capitalized. But the result is different for str4. As str4 variable had a value of '1python 2code to 2explain title method', the title() method avoids the first character that is numeric and capitalizes the second alphabet.

As you can see in the above example that title() function also convert the first character of such word is, as, that, was, on, of, if, in, a, am, the” in Upper case. We can avoid such keyword to convert their first character into uppercase by creating a custom function.

Example 2

Python Code To Explain Custom title() Method

# Python program to explain custom title function
# Define new function
def MyTitle(NewWord):
    # Initialize a list with skip words
    SkipWords = ['is', 'as', 'that', 'was', 'on', 'of', 'if', 'in', 'a', 'am', 'the', 'to', 'my']
    # Check if string has skip words
    if NewWord not in SkipWords:
        # Capitalized word
        return NewWord.capitalize()
    return NewWord
# Initialize our string to convert to Uppercase
MyString = 'This is my python code to explain the working of title method'
# Split String with space and save words in list 'MyList'
MyList = MyString.split(" ")
# Pass list 'MyList' to MyTitle function
nList = map(MyTitle, MyList)
# Join return string with space
print(" ".join(nList))

Output:

This is my Python Code to Explain Working of Title Method

Explanation

In this code, a function MyTitle(keyword) is defined that takes a parameter keyword. A List “SkipWords” is initialized with a list of strings ['is', 'as', 'that', 'was', 'on', 'of', 'if', 'in', 'a', 'am', 'the', 'to', 'my']. In the next line, if NewWord not in SkipWords: the if statement checks whether the NewWord resides in list SkipWords. If the condition evaluates to True, the function returns this variable after capitalizing it by the capitalize() method. Then the value is stored in the local variable NewWord. The function returns the NewWord variable value after the if condition stops.

Then, outside the function, a variable called MyString is initialized with the value 'This is my python code to explain the working of title method'. The characters in the variable are split using the split() method. The characters are split based on the separator “ ” mentioned in split() and the value is stored in a variable MyList.

Then a map() method is used to pass the MyList variable values to MyTitle() function. So in the line, nList = map(MyTitle, MyList) the MyTitle() method is applied to all elements of the MyList list. The resultant values are stored in the nList variable.

The last line print(" ".join(nList)) prints the elements of the nList after joining them with space in between. The final result is

This is my Python Code to Explain Working of Title Method

Conclusion

Apart from the title() method, in case you want to convert all characters of a string into uppercase, the upper() method in Python will be useful. For converting all characters of a string into lowercase, use the lower() method.