Python String capitalize()

In Python, the capitalize() method capitalizes a string i.e. upper case the very first letter in the given string and lowercases all other characters, if any.

Except changing the case (upper/lower), capitalize() does not modify the content of the original string.

1. capitalize() Syntax

The syntax of capitalize() is pretty simple.

string.capitalize()

2. capitalize() Example

Lets see few examples to understand the usage of capitalize() in Python.

Example 1: Capitalizing when first character is alphabet

string = "god is Great"

capitalizedString = string.capitalize()

print('Old String: ', string)
print('Capitalized String:', capitalizedString)

Program output.

Old String:  god is Great
Capitalized String: God is great

Example 2: Capitalizing when first character is non-alphabet

Notice how the capital ‘N’ has been converted to small ‘n’. It concludes that even if first character is non-alphabet, capitalize() method still checks the whole string and make them lowercase.

string = "0 is most powerful Number"

capitalizedString = string.capitalize()

print('Old String: ', string)
print('Capitalized String:', capitalizedString)

Program output.

Old String:  0 is most powerful Number
Capitalized String: 0 is most powerful number

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.