Both title() and capitalize() have similar functionality of capitalizing first characters. Let us see the difference between the two of them.
Method 1: title()
title() function in Python is the Python String Method which 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.
Syntax: str.title()
Parameters: None
Returns: This function returns a string which has first letter in each word is uppercase and all remaining letters are lowercase.
Example:
Python3
# Python Title() Method Example str1 = 'geeKs foR geEks' str2 = str1.title() print ( 'First Output after Title() method is = ' , str2) # observe the original string print ( 'Converted String is = ' , str1.title()) print ( 'Original String is = ' , str1) # Performing title() function directly str3 = 'ASIPU pawan kuMAr' .title() print ( 'Second Output after Title() method is = ' , str3) str4 = 'stutya kUMari sHAW' .title() print ( 'Third Output after Title() method is = ' , str4) str5 = '6041' .title() print ( 'Fourth Output after Title() method is = ' , str5) |
Output:
First Output after Title() method is = Geeks For Geeks Converted String is = Geeks For Geeks Original String is = geeKs foR geEks Second Output after Title() method is = Asipu Pawan Kumar Third Output after Title() method is = Stutya Kumari Shaw Fourth Output after Title() method is = 6041
Method 2: capitalize()
In Python, the capitalize() method converts the first character of the first word of a string to a capital (uppercase) letter.
Syntax:
str.capitalize()
Parameters: NA
Return Type: This function returns a string that has the first letter of the first word in uppercase and all remaining letters of the first word in lowercase.
Example:
Python3
# Python program to demonstrate the # use of capitalize() function # capitalize() first letter of # string. name = "Lazyroar for Lazyroar" print (name.capitalize()) # demonstration of individual words # capitalization to generate camel case name1 = "Lazyroar" name2 = "for" name3 = "Lazyroar" print (name1.capitalize() + name2.capitalize() + name3.capitalize()) |
Geeks for Lazyroar GeeksForGeeks
Difference Between title() and capitalize()
The difference between them is that Python string method title() returns a copy of the string in which the first characters of all the words are capitalized whereas the string method capitalize() returns a copy of the string in which just the first word of the entire string is capitalized.
Example:
str = "Lazyroar for Lazyroar" str.title() will return Geeks For Geeks str.capitalize() will return Geeks for Lazyroar
Example
Python3
str1 = "my name is xyz" str2 = "Lazyroar for Lazyroar" # using title() print (str1.title()) print (str2.title()) # using capitalize() print (str1.capitalize()) print (str2.capitalize()) |
My Name Is Xyz Geeks For Geeks My name is xyz Geeks for Lazyroar
Let us see the differences in a tabular form as follows:
str.capitalize() | str.title |
The str.capitalize() function is used to convert strings in the Series/Index to be capitalized | The str.title() function is used to convert strings in the Series/Index to titlecase. |
Its syntax: str.capitalize() |
Its syntax: str.title(); |
It does not take any parameters | It does not take any parameters |
Its return type is String. | Its return type is string. |