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"

str2="i love i love"

str3="17 is my magic number"



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"

str2="i love i love"



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".capitalize())

print("i love i love".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"

str2="i love i love"



Leave a Comment