Capitalize() in Python

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:

# 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:

Curiouser and curiouser!

Begin at the beginning

 

Example 2:

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

# 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:

Name : džamonja

Capitalized name : Džamonja

 


Copyright 2024 © pythontic.com