7 Ways in Python to Capitalize First Letter of a String

Hello coders!! In this article, we will be learning how one can capitalize the first letter in the string in Python. There are different ways to do this, and we will be discussing them in detail. Let us begin!

Method 1: str.capitalize() to capitalize the first letter of a string in python:

  • Syntax: string.capitalize()
  • Parameters: no parameters
  • Return Value: string with the first capital first letter
string = "python pool"
print("Original string:")
print(string)
print("After capitalizing first letter:")
print(string.capitalize())

Output & Explanation:

str.capitalize() to capitalize
Output

When we use the capitalize() function, we convert the first letter of the string to uppercase. In this example, the string we took was “python pool.” The function capitalizes the first letter, giving the above result.

Method 2: string slicing + upper():

  • Synatx: string.upper()
  • Parameters: No parameters
  • Return Value:  string where all characters are in upper case
string = "python pool"
print("Original string:")
print(string) 
result = string[0].upper() + string[1:]
print("After capitalizing first letter:")
print(result)

Output & Explanation:

string slicing + upper
Output

We used the slicing technique to extract the string’s first letter in this example. We then used the upper() method to convert it into uppercase.

Method 3: str.title():

  • Syntax: str.title()
  • Parameters: a string that needs to be converted
  • Return Value: String with every first letter of every word in capital
string = "python pool"
print("Original string:")
print(string)
print("After capitalizing first letter:")
print(str.title(string))

Output & Explanation:

str.title to python capitalize first letter
Output

str.title() method capitalizes the first letter of every word and changes the others to lowercase, thus giving the desired output.

Method 4: capitalize() Function to Capitalize the first letter of each word in a string in Python

string = "python pool"
print("Original string:")
print(string)
print("After capitalizing first letter:")
result = ' '.join(elem.capitalize() for elem in string.split())
print(result)

Output & Explanation:

capitalize() Function to Capitalize the first letter of each word in a string in Python
Output

In this example, we used the split() method to split the string into words. We then iterated through it with the help of a generator expression. While iterating, we used the capitalize() method to convert each word’s first letter into uppercase, giving the desired output.

Method 5: string.capwords() to Capitalize first letter of every word in Python:

  • Syntax: string.capwords(string)
  • Parameters: a string that needs formatting
  • Return Value: String with every first letter of each word in capital
import string
txt = "python pool"
print("Original string:")
print(txt)
print("After capitalizing first letter:")
result = string.capwords(txt)
print(result)

Output & Explanation:

string.capwords() to Capitalize first letter of every word in Python
Output

capwords() function not just convert the first letter of every word into uppercase. It also converts every other letter to lowercase.

Method 6: Capitalize the first letter of every word in the list in Python:

colors=['red','blue','yellow','pink']
print('Original List:')
print(colors)
colors = [i.title() for i in colors]
print('List after capitalizing each word:')
print(colors)

Output & Explanation:

Capitalize first letter of every word in list
Output

Iterate through the list and use the title() method to convert the first letter of each word in the list to uppercase.

Method 7:Capitalize first letter of every word in a file in Python

file = open('sample1.txt', 'r') 
for line in file: 
    output = line.title() 
 
    print(output)

Output & Explanation:

Python Pool Is Your Ultimate Destination For Your Python Knowledge

We use the open() method to open the file in read mode. Then we iterate through the file using a loop. After that, we capitalize on every word’s first letter using the title() method.

You might be interested in reading:

Conclusion:

The various ways to convert the first letter in the string to uppercase are discussed above. All functions have their own application, and the programmer must choose the one which is apt for his/her requirement.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments