Python String capitalize()

Python string capitalize() method will convert the first letter in a string to uppercase and keep the rest of the characters in lowercase. The capitalize() function does not modify the original string and instead returns a string copy.

Syntax

The syntax of capitalize() method is:

string.capitalize()

Parameter

The capitalize() function does not take any parameters.

Return Value

The capitalize() function returns a copy of the string with the first character capitalized and all other characters lowercased. It doesn’t modify the original string.

Note: In Python 3.8 onwards, the first character is converted into a title case rather than uppercase. It means that characters like digraphs will only have their first letter capitalized instead of the whole character.

Example: Capitalize a string in Python


# Converts the first character to Uppercase/title case
# keeps rest in lowercase
text1 = "python programming"
print(text1.capitalize())

# Converts the first character to Uppercase/title case
# keeps rest in lowercase
text2= "pYTHON Is FUN"
print(text2.capitalize())

# In case of diagraph the first letter is capitalized
text3= "ß"
print(text3.capitalize())


# In case of non alphabets
text4= "*disclaimer"
print(text4.capitalize())

Output

Python programming
Python is fun
Ss
*disclaimer
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like