Home » Title in Python

Title in Python

The title() method is used in Python to convert the first letter of the string to uppercase.

In this article, we will discuss different programs to comprehend it’s working.

  1. The Basic Approach
  2. Title() with NumPy
  3. Title() with Pandas

The Basic Approach

So let’s begin with the first program.

Output:

String1 after conversion is:  Software Development Life Cycle  The original string is:  soFTwaRe DevElOpMent LiFe CycLe  String3 after conversion is:  Artificial Intelligence  String4 after conversion is:  Data Science  String5 after conversion is:  Machine Learning  

Explanation-

Let’s have a glance at the explanation of this program,

  1. In the first step, we have initialized a string that contains letters in both uppercase and lowercase in a random fashion.
  2. After this, we have applied the method title() on string1 and stored it in string2.
  3. Finally, to understand how it can be used directly with a string, we have declared three strings namely string3, string4, and string5, and then applied title() on them.
  4. On executing the program, we can observe that the first letter of all the strings is converted to an uppercase.

In the second program, we shall see what will happen if it is used with a string that contains an apostrophe.

Output:

String1 after conversion is:  Kenny'S Notebook Is With John  The original string is:  KeNNy's noTeBoOk iS wiTh JOhN  String3 after conversion is:  Let'S Celebrate The Colour Of Lights  String4 after conversion is:  Jimmy'S Favourite Is Subject Is Chemistry.  String5 after conversion is:  It'S Raining Today  

Explanation-

It’s time to understand what we have done in the above program,

The approach is similar to the above program but in the above program only the first letters were converted to uppercase whereas here letter present after the apostrophe is also converted, this problem can be solved using regular expressions.

Let us now see how title() can be used with NumPy.

Title() with NumPy

In the third program, we will learn how NumPy can be used to convert the first letter to uppercase.

The following program illustrates the same,

Output:

The values in the original array are:  ['soFTwaRe DevElOpMent LiFe CycLe' 'ArtIfIcIAl IntEllIGence'   'DaTa scIEncE' 'mAcHiNe lEaRnInG']  The converted array is:  ['Software Development Life Cycle' 'Artificial Intelligence'   'Data Science' 'Machine Learning']  

Explanation-

  1. First of all, we have imported the NumPy module and then created an array with different string values.
  2. After this, we have used the method np.char.title() in which we passed our array.
  3. Finally, on executing the program, we can observe that it displays the desired output.

At last, we will see the title() using pandas in Python

Title() with Pandas

To use the title() with pandas, we have taken a csv file that contains the student details-

Title in Python

The program given below shows how it can be used,

Output:

Student     Details                       Subject Code  0           Kim:Networking                129901  1           Peter:Software Engineering    129902  2           Rohan:Python Programming      129903  3           John:Operating Systems        129904  4           Sylvester:Data Structures     129905  

Explanation-

  1. In the first step, we have imported the pandas module.
  2. After this, we have read the csv file that contains the details of students.
  3. Finally, we have used str.title() to convert the first letter of the string to uppercase.
  4. On executing the program, the expected output is displayed.

Conclusion

In this article, we learned how the title() method can be used in Python.


You may also like