Skip to content

How to capitalize the first letter in Python | Example code

  • by

Use capitalize() method If you want to capitalize the only first letter of a given sentence in Python. This method returns a copy of the original string and converts the first character of the string to capital and the rest of all other characters in the string in lowercase letters.

Example capitalize the first letter in Python

Simple example code capitalizes the first letter only. If the first character is an integer, it will not capitalize the first letter.

txt = 'the brOwn Fox'

res = txt.capitalize()

print(res)

Output:

How to capitalize the first letter in Python

Use title() function Capitalize the first letter of each word in a string in Python.

txt = 'the brOwn Fox'

res = txt.title()

print(res)

Output:

The Brown Fox

Comment if you have any doubts or suggestions on this Python capital topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

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