Python String capitalize() Method Tutorial

Strings contain multiple characters which can be capital letters or lower letters. Capitalization means making the first letter or character of a sentence or string uppercase where other letters or characters stay the same.

Capitalize String with capitalize() Method

The capitalize() method is very simple to use. This method will be called via string object or literal where the first letter of the given string will be converted into the uppercase. Even the given string contains multiple sentences only the first letter will be made uppercase.

str1="i love pythontect.com"

str2="i love pythontect.com. i love pythontect.com"

str3="17 is my magic number"

print(str1.capitalize())

print(str2.capitalize())

print(str3.capitalize())

Capitalize String Manually

Python is a dynamic language where it provides different ways to accomplish a task. The string is a list of characters in the PYthon and the slicing operation can be used in order to select specific parts of characters of the string. We can select the first character of the string and convert into uppercase and join another part of the string in order to capitalize. We will use the upper() method which makes given characters uppercase.

str1="i love pythontect.com"

str2="i love pythontect.com. i love pythontect.com"

print(str1[0].upper()+str1[1:])

print(str2[0].upper()+str2[1:])

Decapitalize a String

We may also need to decapitalize a given string which means the first letter or character will make lowercase. We can use the manual method where the first letter is selected with slice operation and provided to the lower() method which will make given characters lower case.

str1="i love pythontect.com"

str2="i love pythontect.com. i love pythontect.com"

print(str1[0].lower()+str1[1:])

print(str2[0].lower()+str2[1:])

Leave a Comment