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.
- The Basic Approach
- Title() with NumPy
- 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,
- In the first step, we have initialized a string that contains letters in both uppercase and lowercase in a random fashion.
- After this, we have applied the method title() on string1 and stored it in string2.
- 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.
- 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-
- First of all, we have imported the NumPy module and then created an array with different string values.
- After this, we have used the method np.char.title() in which we passed our array.
- 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-
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-
- In the first step, we have imported the pandas module.
- After this, we have read the csv file that contains the details of students.
- Finally, we have used str.title() to convert the first letter of the string to uppercase.
- On executing the program, the expected output is displayed.
Conclusion
In this article, we learned how the title() method can be used in Python.