Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23137

Python Pool: 7 Ways in Python to Capitalize First Letter of a String

$
0
0

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 capitalizeOutput

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 
print("After capitalizing first letter:")
print(result) 

Output & Explanation:

string slicing + upperOutput

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 letterOutput

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 PythonOutput

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 PythonOutput

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 listOutput

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!

The post 7 Ways in Python to Capitalize First Letter of a String appeared first on Python Pool.


Viewing all articles
Browse latest Browse all 23137

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>