How to capitalize first letter in Python?
Python is the most famous programming language in the world. The reason behind its popularity is the wide variety of programming features. Python has easy-to-understand syntax, which makes it a great language for beginners to learn. There are many features that can be used to perform different tasks. In today’s article, we will be covering how to capitalize first letter in Python
Introduction
Among the variety of different features, one such is the ability to change the case of letters in a string. When given a string, we can use the following functions to alter the beginning letter of the sentence or each word of the phrase to lowercase or uppercase.
Methods to Capitalize the First Letter
There are several ways to capitalize in python. You can either capitalize only the first word of a given string or capitalize every word of a given string. In this article we will be mainly discussing the following ways:-
- Using the
capitalize()
function - Using the string-slicing method and
upper()
function - Using the
title()
function - Using the
capwords()
function
Using the capitalize() function
In this method, we use Python’s in-built function capitalize()
. To use this, we first set a variable named "my_string"
in which we will be storing the string, and now we will use the function whose logic goes as follows:-
First lowercase all the letters in the string, then uppercase the first letter of the string, and at last return a copy of the string. We can use this function with the print statement to get the desired output.
Note: The original string (i.e. "my_string"
) is unaffected by the capitalize()
method.
Using the string slicing method and upper() function
In this method, suppose you are a string, say “winner” stored in the variable "word"
Now you have stated you cannot use capitalize()
, title()
, or capwords()
. We will capitalize the word using logic. The logic is as follows:-
We have a variable with a string in it.
First, we will slice the string (using the slice method) along the first letter and the remaining word; after this, we will make the first letter uppercase (using the upper()
function); and then we will concatenate the sliced word into the original string and print it.
Using the title() function
For this method, we use Python’s built-in function title()
. First, we set a variable named "sentence_1"
which will store our string. Now, we use the function whose logic goes as follows: break up each word in the sentence, capitalize the word’s first letter, and then concatenate
(join) the word and return the copy of the string. We can store the modified string in another variable (say "sentence_2"
) and print it.
Note: The title()
function makes no changes to the existing original string (i.e. "sentence_1"
)
Using the capwords() function
The function capwords()
provides the same functionality and logic as the previous function. But the point to be noted is that you will have to import the string module before using this function, and the syntax for this function is a bit different. Follow the code down below.
Capitalize the first letter in Lists and Text files
Now that you know how to capitalize the first letter of a string, you may be wondering how to capitalize if you are given a list with multiple elements or a text file. Don’t worry, we have a solution for these as well.
Lists
For lists, let’s assume we have a list "List_1"
consisting of 5 string elements. The logic for capitalizing the first letter of each element is as follows:
First, we are given a list with 5 string elements. Now we declare a new list "List_2"
in which we will store the modified elements. To do this, we run a for loop with the "List_1"
and then use the title()
function to capitalize each word element and use the append()
function to add it to the "List_2"
.
Text Files
For text files, we will first open the text file in read mode in Python with the variable "File"
, then, using a for loop, execute each line of the file and then capitalize each word of the line using the title()
function and display the result.
Conclusion
And here we end with today’s article. We talked about applying the string slicing
+ upper()
method, the capitalize()
function, the title()
function, the capwords()
function and capitalizing letters in lists and text file to capitalize the initial letter in Python. I hope this article was useful to you. If you have any questions, please leave them in the comments section. Thank you for reading!
You can learn more functions of Python and master them in the course Learn Python 3 From Scratch: Python Basics And Fundamentals
Sharing is caring
Did you like what Pravin Gupta wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses:
302 students learning
Haris
Python Crash Course for Beginners
Surendra varma Pericherla
Learn Data Structures Using Python