Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python String title() method

Python String Title() Method

In this article, we will understand the functionality of String title() method. Python String contains a huge number of built-in functions to work with data. We also recently worked on the istitle() method which confirms the title case for a string.


Getting started with Python String title() method

Python String title() method basically converts the input string to title case i.e. it converts only the first character of every word in the input string to Uppercase and converts the rest of the characters to Lowercase.

Syntax:

input_string.title()

Example:

inp1 = 'Amazon is a wonderful platform.'
res1 = inp1.title() 
print(res1) 

In this example, as seen above, only the first character of every word in the input string is converted to Uppercase.

Output:

Amazon Is A Wonderful Platform.

Example 2:

inp1 = 'AMAZON IS A WONDERFUL PLATFORM.'
print("Input String:\n",inp1)
res1 = inp1.title() 
print("Converted String:\n",res1)

All the other characters other than the first alphabet are converted to lowercase as seen above.

Output:

Input String:
 AMAZON IS A WONDERFUL PLATFORM.
Converted String:
 Amazon Is A Wonderful Platform.

Example 3:

inp1 = '21wellness'
print("Input String:\n",inp1)
res1 = inp1.title() 
print("Converted String:\n",res1)

The presence of digits or number before the word doesn’t affect the working of the function. The character after the digit is considered as the first character.

Output:

Input String:
 21wellness
Converted String:
 21Wellness

NumPy title() method

NumPy module has numpy.char.title() function to title case the input data.

The numpy.char.title() method converts the first alphabet of every element of the input array to Uppercase and the rest of the characters of every word to Lowercase in an element-wise fashion.

Syntax:

numpy.char.title(input_array)

Example:

import numpy

inp = numpy.array(['TAJ', 'mahaL', '4$road', 'senTosA']) 
print ("Input Array:\n", inp) 

res = numpy.char.title(inp) 
print ("Resultant Array:\n", res) 

Output:

Input Array:
 ['TAJ' 'mahaL' '4$road' 'senTosA']
Resultant Array:
 ['Taj' 'Mahal' '4$Road' 'Sentosa']

Pandas title() method

Pandas module has a built-in Series.str.title() method to title case every item of the input data set.

Syntax:

Series.str.title()

The Series.str.title() method converts the first alphabet of every element of the data set to Uppercase and the rest of the characters of every word to Lowercase in an element-wise fashion.

Input csv file:

Inputfile-title()
Input file-title()

Example:

import pandas
inp = pandas.read_csv("C:\\Users\\HP\\Desktop\\Book1.csv") 
inp["Details"]= inp["Details"].str.title() 
inp 

Output:

        Details	        Number
0	John:Pune	21
1	Bran:Satara	22
2	Sam:Chennai	24
3	Rhey:Delhi	12
4	Cranny:Karnatak	26

Conclusion

In this article, we have understood the working of Python title() function.


References

Python String title() method