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 !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode