capitalize() in Python

The capitalize() method of str class in Python returns a copy of the string that has its first letter capitalised and all other letters made to lower case.

Str Updated 9 Mar 2017 1 min read

Method Name

capitalize

Method signature

capitalize()

Method Overview

  • The capitalize() method of Python str class returns a copy of a python string object with the following modifications done:
    • The first character of the string is capitalized
    • All other characters of the string except the first character are made lower case.
    • If the first character is a digraph it will be titlecased.ie., Only the first character of the digraph is made upper case.

Example 1

python
# String with lower case and upper case letters

aliceQuote = "Curiouser And Curiouser!";

print(aliceQuote.capitalize())

# String with all lower case letters

aliceQuote = "begin at the beginning";

print(aliceQuote.capitalize())

Output

Output
Curiouser and curiouser!

Begin at the beginning

##

Example 2

The example capitalizes a serbian name "džamonja". The first letter is a digraph - Dž. The capitalize() method capitalizes only the first letter of the character "dž".

python
# Example Python program that capitilizes

            # a string that starts with a digraph

# Serbian name

            name = "džamonja"

            capitalized = name.capitalize()

            print("Name : %s"%name)

            print("Capitalized name : %s"%capitalized)

Output

Output
Name : džamonja

Capitalized name : Džamonja