In this article, we will learn how to capitalize the first letter of each word of a string in python.
There are various ways to capitalize the first letter of each word of a string in python. Here we are using the title()
, capwords()
method and other methods to capitalize on the first letter of each word of a string in python.
Example 1: By using title()
method
# How To Capitalize First Letter of Each Word of a String in Python
# Using title() method of Python
str1='The quick brown fox jumps over the lazy dog'
print(str1.title())
# Output ==> The Quick Brown Fox Jumps Over The Lazy Dog
str2='THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG'
print(str2.title())
# Output ==> The Quick Brown Fox Jumps Over The Lazy Dog
str3='THE quick Brown Fox JuMp over the LAZy dog'
print(str3.title())
# Output ==> The Quick Brown Fox Jumps Over The Lazy Dog
# In this case, title() method is not works well
str4="I can't believe it's snowing again."
print(str4.title())
# Output ==> I Can'T Believe It'S Snowing Again.
Example 2: By using the capwords()
method. For using this method, we need to import the string
library in our source code.
# How To Capitalize First Letter of Each Word of a String in Python
# using capswords() Method
import string
str1="The quick brown fox jumps over the lazy dog"
print(string.capwords(str1))
# Output ==> The Quick Brown Fox Jumps Over The Lazy Dog
str2="THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
print(string.capwords(str2))
# Output ==> The Quick Brown Fox Jumps Over The Lazy Dog
str3="THE quick Brown Fox JuMp over the LAZy dog"
print(string.capwords(str3))
# Output ==> The Quick Brown Fox Jumps Over The Lazy Dog
str4="I can't believe it's snowing again."
print(string.capwords(str4))
# Output ==> I Can't Believe It's Snowing Again.
str5="I can'T believe it'S snowing again."
print(string.capwords(str5))
# Output ==> I Can't Believe It's Snowing Again.
Example 3: By using Join()
and split()
method
# How To Capitalize First Letter of Each Word of a String in Python
#using Join() and split() method
str1 = "The quick brown fox jumps over the lazy dog"
str1 = ' '.join(word[0].upper() + word[1:] for word in str1.lower().split())
print(str1)
# Output ==> The Quick Brown Fox Jumps Over The Lazy Dog
str2 = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
str2 = ' '.join(word[0].upper() + word[1:] for word in str2.lower().split())
print(str2)
# Output ==> The Quick Brown Fox Jumps Over The Lazy Dog
str3 = "THE quick Brown Fox JuMp over the LAZy dog"
str3 = ' '.join(word[0].upper() + word[1:] for word in str3.lower().split())
print(str3)
# Output ==> The Quick Brown Fox Jumps Over The Lazy Dog
str4 = "I can't believe it's snowing again."
str4 = ' '.join(word[0].upper() + word[1:] for word in str4.lower().split())
print(str4)
# Output ==> I Can't Believe It's Snowing Again.
str5 = "I can'T believe it'S snowing again."
str5 = ' '.join(word[0].upper() + word[1:] for word in str5.lower().split())
print(str5)
# Output ==> I Can't Believe It's Snowing Again.
I hope this article will help you to understand how to capitalize the first letter of each word of a string in python.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments