str.title
Возвращает копию строки, в которой каждое новое слово начинается с заглавной буквы и продолжается строчными.
В результирующей строке первая буква каждого нового слова становится заглавной, в то время как остальные становятся строчными. Такое написание характерно для заголовков в английском языке.
'кот ОбОрмот!'.title() # Кот Обормот!
"they're bill's friends from the UK".title()
# They'Re Bill'S Friends From The Uk
Алгоритм использует простое, независящее от языка определение слова — это группа последовательных букв. Такого определения во многих случаях достаточно, однако, в словах с апострофами (в английском они используются, например, в сокращениях и притяжательных формах) оно приводит к неожиданным результатам (см. пример выше). И в таких случаях лучше всего будет воспользоваться методом замены в регулярных выражениях (см. модуль re ).
Для 8-битных строк (Юникод) результат метода зависит от текущей локали.
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.
str.capitalize()
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.
Syntax
str_name.capitalize()
Here, str_name is the input string that is to be capitalized. The capitalize() method takes no arguments.
Example
str = 'hello world' print(str.capitalize())
Output
Hello world
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.
str.title()
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.
Syntax
str_name.title()
Here, tr_name is the string that is to be modified. The title() method takes no arguments, the same as capitalize() as seen before.
Example
s = 'hello world' print(s.title())
Output
Hello World
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.
Example
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.
Algorithm
- 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.
Example
name = "john doe" print(name.title()) print(name.capitalize()) sentence = "this is a sentence." print(sentence.title()) print(sentence.capitalize())
Output
John Doe John doe This Is A Sentence. This is a sentence.
Summary capitalize() vs title()
| str.capitalize() | str.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. |
| Syntax | str_name.capitalize() | str_name.title() |
| Return Type | String | String |
Conclusion
Hence both capitalize() and title() methods are useful in different situations. Therefore, which method to use depends on the specific use case.
Python String title()
Python includes a wide range of string handling functions. String handling methods include converting a string to lowercase, uppercase, and so on. Another such method is the title case method. Python title() function is used to convert an input string to a title case i.e. converting the first alphabet of each word to uppercase and the rest into lowercase. Let us understand about Python title() in the article below.

Table of content
Definition
- The Python title() function is used to change the initial character in each word to Uppercase and the subsequent characters to Lowercase and then returns a new string.
- Python title()method returns a title-cased string by converting the initial letter of each word to a capital letter.
title() Syntax
Python title() function follows the below-mentioned syntax:
string.title()title() Parameters
This function does not accept any parameter. If any parameter is passed, the function throws an exception.
Return value from title()
The title() string function returns a new string that contains a title-cased version of the input string. In Layman terms, this function capitalizes the first letter of each word in the string.
Note – Python title() function ignore any non-alphabetic characters. It ignores any numbers and special characters in the string.
Example 1: How Python title() works?
# Python program to illustrate title() my_str = 'Hello, welcome to the museum' print(my_str.title()) my_str = '101 hacks for python' print(my_str.title()) my_str = 'I AM an MBA STUdent' print(my_str.title()) my_str = '262131 @%&*' print(my_str.title())Hello, Welcome To The Museum 101 Hacks For Python I Am An Mba Student 262131 @%&*Example 2: title() with apostrophes
This function utilizes a straightforward, language-independent definition of a word as groups of consecutive letters. As a result, apostrophes (‘) are treated as a word boundary.
When using apostrophes and numbers in between words, the Python title() method may show unexpected and strange behavior. The initial letter following any number or special character (such as an apostrophe) is changed to an upper-case letter. Look at the below example to understand more:
# Python program to illustrate title() my_str = "He's here, isn't he? " print(my_str.title()) my_str = 'Pyt0n Progr6mm1ng' print(my_str.title())He'S Here, Isn'T He? Pyt0N Progr6Mm1NgWe don’t want this to happen most of the time. You may use regex or the string.capwords() to fix this problem.
Example 3: Using Regex or string.capwords() to Title Case String
# Using regex import re def titlecasefunct(s): return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(), s) txt = "They're Jimmy's friends. Let's give them 10dollars each" print(titlecasefunct(txt)) print('') # Using capwords() import string s = "I'll be ca11ing you, won't I?" val = string.capwords(s) print(val)They're Jimmy's Friends. Let's Give Them 10Dollars Each I'll Be Ca11ing You, Won't I?Frequently Asked Questions
Q1. What is title() in Python?
A title-cased string is the one in which every first letter of the word is an uppercase character. Python title() function is used to convert an input string to a title case i.e. converting the first alphabet of each word to uppercase and the rest into lowercase. Python title() function ignore any non-alphabetic characters. It ignores any numbers and special characters in the string.
Q2. How do you add a title in Python?
To add a title to an input string in Python, we implement the title() string class method. Python title() function follows the below-mentioned syntax:
Any difference between str.capitalize() and str.title()?
Is there any difference in str.title() vs str.capitalize() ? My understanding from the docs is that both methods capitalize the first letter of a word and make the rest of the letters lowercase. Did anyone run into a situation where they cannot be used interchangeably?
asked Dec 18, 2019 at 3:01
1,023 2 2 gold badges 9 9 silver badges 19 19 bronze badges3 Answers 3
title() changes every word, but capitalize() only the first word in a sentence:
>>> a = 'nice question' >>> a.title() 'Nice Question' >>> a.capitalize() 'Nice question' >>>59k 17 17 gold badges 168 168 silver badges 148 148 bronze badges
answered Dec 18, 2019 at 3:06
23.3k 4 4 gold badges 34 34 silver badges 43 43 bronze badgesYep, there's a difference. 2, actually.
- In str.title() , where words contain apostrophes, the letter after the apostrophe will be capitalised.
- str.title() capitalises every word of a sentence, whereas str.capitalize() capitalises just the first word of the entire string.
str.title()
Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.
For example:
>>> 'Hello world'.title() 'Hello World'The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result: