- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between str.capitalize() vs str.title()
In Python, String is a sequence of characters surrounded by either double quotes(" ") or single quotes (' '). Strings are used to represent text data in Python and it can contain letters, numbers, and symbols.
String data type is immutable in Python, that is once a string instance is created it's value cannot be changed. But a new string with the required changes made in the original string can be created.
Python Strings come with numerous methods like capitalize(), upper(), title(), split(), strip(), join(), etc. that can also be used to manipulate strings.
When the Python method capitalize() is called, the input string capitalises the first letter of the first word. In return, this method gives a string that has the first letter of the word in uppercase and leaves the remainder of the input string unchanged. The method will produce a new string with the first letter capitalized but does not change the original input string.
Here, str_name is the input string that is to be capitalized. The capitalize() method takes no arguments.
str = 'hello world' print(str.capitalize())
As previously mentioned, the capitalise() method is particularly helpful for capitalizing the first letter in a string while changing the case of every subsequent letter.
The Python method title() is a String method that is used to convert the first character in every word of the input string to uppercase and the remaining all characters to lowercase. title() returns a new string in which just the first character of words is uppercase.
The title() method's ability to keep the original input string is a key feature. But instead, it returns a new modified string. Hence, if the original string need to be altered, then the altered string should be allocated to the original string.
Here, tr_name is the string that is to be modified. The title() method takes no arguments, the same as capitalize() as seen before.
s = 'hello world' print(s.title())
As illustrated in the above-given example code, the String method title() not only capitalizes the first letter of every word in the input string but also changes all remaining letters to lowercase.
Although both the methods, capitalize() and title(), are currently used to alter the strings, the crucial difference between them is how they convert the said string.
The methods capitalize() and title() differ in that the former only changes the string's first character, whilst the latter updates the first character of each word in the given input string, as was already mentioned above. Irrespective of how many words are in a string, the capitalise() method will only modify the initial character. In contrast, the title() method capitalises the first letter of each word in the string.
The way that title() and capitalise() handle non-alphabetic characters is another difference. The capitalize() method capitalizes the alphabetic character in a string and converts all other characters to lowercase. Any non-alphabetic characters present there are unaffected. But the title() method retains uppercase characters within a string. Therefore if there are already uppercase characters in a string using the title() method will not convert them to lowercase.
To understand the difference better let us see a code where we use the title() method for a string representing a name and capitalize() method on a sentence.
Initialize a string variable 'name' with the value "john doe".
Convert the string variable 'name' to title using title() method and print the result.
Convert 'name' to capitalize case using the capitalize() method and print the result.
Initialize a string variable 'sentence' with the value "this is a SENTENCE."
Convert the value of the string variable 'sentence' to title using title() method and print the result.
Convert 'sentence' to capitalize case using the capitalize() method and print the result.
name = "john doe" print(name.title()) print(name.capitalize()) sentence = "this is a sentence." print(sentence.title()) print(sentence.capitalize())
John Doe John doe This Is A Sentence. This is a sentence.
Summary capitalize() vs title()
|Definition||Capitalizes the first character of the string and converts the rest to lowercase.||Capitalizes the first character of each word in the string and converts the rest to lowercase.|
Hence both capitalize() and title() methods are useful in different situations. Therefore, which method to use depends on the specific use case.
Kickstart Your Career
Get certified by completing the courseGet Started