Python String title()
In Python, String title()
method, when applied on to a string, makes its first letter in Capital or Uppercase and rest of letters in Lowercase As we have seen in headings and title of many articles.
-
title()
is an inbuilt function in python used for string handling.
-
This method converts the first letter of a string as a capital letter (Uppercase) and rest all letters in lowercase or small letters and returns the new string.
-
The new string that is returned is known as title cased (a string whose first letter is in Uppercase while rest are in Lower case).
Python String title()
: Syntax
Below we have a basic syntax of String title()
in Python:
string.title()
Note: string in the above case denotes the value of the string variable that is needed to be title cased.
Python String title()
: Parameters
As from the syntax, it is clear that this method does not take any parameters:
-
If any parameter is passed into this then this will raise an error.
-
In this method, we use a string with a dot operator to show the output.
Python String title()
: Returned Values
This method mainly returns a copy of string whose first letter is in uppercase and rest all letters are in Lowercase; then the whole string is known as title cased string
Python String title()
: Basic Example
Below we have an example to show the working of String title()
method:
h1 = "I am A JOHN CENA FAN!"
h2= "I lOvE sTuDyTonight"
print("Original string: ", h1)
print("title(): ", h1.title(), "\n")
print("Original string: ", h2)
print("title(): ", h2.title(), "\n")
In the above example, we have printed the values of original strings as well as title cased strings. The Output for the same is given below:
Original String: I am A JOHN CENA FAN!
title(): I Am A John Cena Fan!
Original String: I LOvE sTuDyTonight
title() : I Love Studytonight
Python String title()
: Another Example
There is a program below where the values are inputted by the user and later on, the title()
method will be applied to them. Let us see the snippet of code:
for i in range(0, 2):
print("Enter the string: ")
a = input()
print("Original String: ", a)
print("title(): ", a.title(), "\n")
The output for the same is given below:
Enter the string:
ArUn KumAr
Original String: ArUn KumAr
title(): Arun Kumar
Enter the string:
ROHit SHarMa
Original String: ROHit SHarMa
title(): Rohit Sharma
Time For Live Example!
Let us look at the live example given below for a better understanding of String title()
:
Summary
In this tutorial, we have learned title()
method with returned values and parameters which is further followed by a live example.