Skip to content
Related Articles

Related Articles

Difference between str.capitalize() VS str.title()

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 09 Jun, 2022
Improve Article
Save Article

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 a string to a capital (uppercase) letter. If the string has its first character as capital, then it returns the original string.

Syntax:

str.capitalize()

Parameters: NA

Return Type: This function returns a string that has the first letter in uppercase and all remaining letters in lowercase. 

Example:

Python3




# Python program to demonstrate the
# use of capitalize() function
 
# capitalize() first letter of
# string.
name = "geeks for geeks"
 
print(name.capitalize())
 
# demonstration of individual words
# capitalization to generate camel case
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"
 
# using title()
print(str1.title())
print(str2.title())
 
# using capitalize()
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 capitalizedThe str.title() function is used to convert strings in the Series/Index to titlecase.

Its syntax: 

str.capitalize()

Its syntax:

str.title();

Its return type is String.It does not take any parameters
It does not take any parametersIts return type is string.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!