Difference between str.capitalize() VS str.title()
Last Updated :
28 Mar, 2023
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
str1 = 'geeKs foR geEks'
str2 = str1.title()
print ( 'First Output after Title() method is = ' , str2)
print ( 'Converted String is = ' , str1.title())
print ( 'Original String is = ' , str1)
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
name = "geeks for geeks"
print (name.capitalize())
name1 = "geeks"
name2 = "for"
name3 = "geeks"
print (name1.capitalize() + name2.capitalize()
+ name3.capitalize())
|
Output
Geeks for geeks
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 = "geeks for geeks"
str.title() will return Geeks For Geeks
str.capitalize() will return Geeks for geeks
Example
Python3
str1 = "my name is xyz"
str2 = "geeks for geeks"
print (str1.title())
print (str2.title())
print (str1.capitalize())
print (str2.capitalize())
|
Output
My Name Is Xyz
Geeks For Geeks
My name is xyz
Geeks for geeks
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. |
Share your thoughts in the comments
Please Login to comment...