python title case string method code example

Example 1: python title case

string = 'this will be title case'
print(string.title())
#output: 'This Will Be Title Case'

Example 2: title case with apostrophe in python

import re
def titlecase(s):
     return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
                   lambda mo: mo.group(0).capitalize(),
                   s)

Example 3: python code for string title

Copy Code# Python code to explain title() Method 
# Initialize a string with all lowercase characters
str1 = 'python code to explain title method'
# Initialize a string with a first uppercase character
str2 = 'Python Code to Explain Title Method'
# Initialize a string with all uppercase character
str3 = 'PYTHON CODE TO EXPLAIN TITLE METHOD'
# Initialize a string with a first numeric character
str4 = '1python 2code to 2explain title method'
# Pass strings with title() method
TitleStr1 = str1.title()
TitleStr2 = str2.title()
TitleStr3 = str3.title()
TitleStr4 = str4.title()
# Print output
print('String 1: ', str1, ' New 1: ',TitleStr1)
print('String 2: ', str2, ' New 2: ',TitleStr2)
print('String 3: ', str3, ' New 3: ',TitleStr3)
print('String 4: ', str4, ' New 4: ',TitleStr4)