Python String capitalize() Method

Python String capitalize() method converts the first character of a string to the capital (uppercase) letter and make all other characters in the string lowercase. If the String has its first character as capital, then it returns the original String.

Syntax

string.capitalize()

Parameters

This method doesn’t contain any parameters.

Return Value

It returns a string that has its first character as capital.

Visual RepresentationVisual Representation of Python String capitalize() Method

Example 1: How does String capitalize() Method Work?

s1="hello my name is ankit"

print(s1.capitalize())

Output

Hello my name is ankit 

Example 2: String contains digits or special charactersVisual Representation of How string.capitalize() Method works when string contains digits or special characters

s1="789hello how are you?"

print(s1.capitalize())

Output

789hello how are you?

Example 3: An empty string

s1= " "

print(s1.capitalize())

Output

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.