The python string title() method returns a copy of the string in which first characters of all the words are capitalized.
The syntax of the method is: str.title();
Key Points :
• Parametric values: The title() function doesn’t take any parameters.
• Return Type: A title version of the string is returned by the title() function. In other terms, the first character of each phrase is capitalized (if the first character is a letter).
• The new string returned is regarded as the title cased.
Below is the python program to demonstrate the title() function:
str = "the best learning resource for Online Education"; print (str.title())
When we run above the program, the outcome is as follows:
The Best Learning Resource For Online Education
We’ll be covering the following topics in this tutorial:
The title() with apostrophes
str = "I'm a developer, Let’s start coding" print(str.title())
When we run above the program, the outcome is as follows:
I’M A Developer, Let’S Start Coding
The string title() method also capitalizes the first apostrophic message. To solve this problem, regex may be used as follows:
Using Regex to Title Case String
import re def Tcase(s): return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(), s) str = "I'm a developer, Let’s start coding" print(Tcase(str))
When we run above the program, the outcome is as follows:
I’m A Developer, Let’S Start Coding
Below are several other functions that we can use to work with string in Python 3