In the previous article, we have discussed Python String split() Method Examples
Python String title() Function:
The title() method returns a string in which the first character of each word is capitalized. As in a header or a title.
If the word contains a number or a symbol, the following letter will be converted to upper case.
Syntax:
string.title()
Parameters: This function doesn’t have any parameters.
Examples:
Example1:
Input:
Given first string = "hello this is btechgeeks" Given second string = "10345 good morning 2a2a btechgeeks"
Output:
The above given first string is : hello this is btechgeeks The given first string after applying title() function: Hello This Is Btechgeeks The above given second string is : 10345 good morning 2a2a btechgeeks The given second string after applying title() function: 10345 Good Morning 2A2A Btechgeeks
Note:
It is worth noting that the first letter after a non-alphabet letter is converted to an upper case letter.
Example2:
Input:
Given first string = "welcome to Python-Programs" Given second string = "HELLO ALL GOOD MORNING"
Output:
The above given first string is : welcome to Python-Programs The given first string after applying title() function: Welcome To Python-Programs The above given second string is : HELLO ALL GOOD MORNING The given second string after applying title() function: Hello All Good Morning
Program for String title() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Apply title() function to the given first string that returns a string in which the first character of each word is capitalized. As in a header or a title.
- Store it in another variable.
- Print the above-given String.
- Print the above-given String after applying the title() function.
- Similarly, do the same for the other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = "hello this is btechgeeks" # Apply title() function to the given first string that returns a string in # which the first character of each word is capitalized. As in a header or a title. # Store it in another variable. rslt_str1 = gvn_fststr.title() # Print the above given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying title() function. print("The given first string after applying title() function:") print(rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = "10345 good morning 2a2a btechgeeks" rslt_str2 = gvn_scndstr.title() print("The above given second string is :", gvn_scndstr) print("The given second string after applying title() function:") print(rslt_str2)
Output:
The above given first string is : hello this is btechgeeks The given first string after applying title() function: Hello This Is Btechgeeks The above given second string is : 10345 good morning 2a2a btechgeeks The given second string after applying title() function: 10345 Good Morning 2A2A Btechgeeks
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Apply title() function to the given first string that returns a string in which the first character of each word is capitalized. As in a header or a title.
- Store it in another variable.
- Print the above-given String.
- Print the above-given String after applying the title() function.
- Similarly, do the same for the other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable. gvn_fststr = input("Enter some Random String = ") # Apply title() function to the given first string that returns a string in # which the first character of each word is capitalized. As in a header or a title. # Store it in another variable. rslt_str1 = gvn_fststr.title() # Print the above given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying title() function. print("The given first string after applying title() function:") print(rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = input("Enter some Random String = ") rslt_str2 = gvn_scndstr.title() print("The above given second string is :", gvn_scndstr) print("The given second string after applying title() function:") print(rslt_str2)
Output:
Enter some Random String = welcome to Python-Programs The above given first string is : welcome to Python-Programs The given first string after applying title() function: Welcome To Python-Programs Enter some Random String = HELLO ALL GOOD MORNING The above given second string is : HELLO ALL GOOD MORNING The given second string after applying title() function: Hello All Good Morning
Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.