1string = 'this will be title case'
2print(string.title())
3#output: 'This Will Be Title Case'
1import re
2def titlecase(s):
3 return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
4 lambda mo: mo.group(0).capitalize(),
5 s)
6
1Copy Code# Python code to explain title() Method
2# Initialize a string with all lowercase characters
3str1 = 'python code to explain title method'
4# Initialize a string with a first uppercase character
5str2 = 'Python Code to Explain Title Method'
6# Initialize a string with all uppercase character
7str3 = 'PYTHON CODE TO EXPLAIN TITLE METHOD'
8# Initialize a string with a first numeric character
9str4 = '1python 2code to 2explain title method'
10# Pass strings with title() method
11TitleStr1 = str1.title()
12TitleStr2 = str2.title()
13TitleStr3 = str3.title()
14TitleStr4 = str4.title()
15# Print output
16print('String 1: ', str1, ' New 1: ',TitleStr1)
17print('String 2: ', str2, ' New 2: ',TitleStr2)
18print('String 3: ', str3, ' New 3: ',TitleStr3)
19print('String 4: ', str4, ' New 4: ',TitleStr4)