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. The capitalize() method only changes the first character and does not change other characters. If other characters are lower case they stay lower case if they are uppercase they stay uppercase.

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 if 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 with capitalize() Method

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 it 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:])

Capitalize String Value or Literal

Python string value or literal can be also capitalized using the capitalize() method. The string value or literal is interpreted as an object and the capitalize() method is provided via this object.

print("i love pythontect.com".capitalize())

print("i love pythontect.com. i love pythontect.com".capitalize())

print("17 is my magic number".capitalize())

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