How to capitalize the first letter of a string in Python

String First Letter Capitalization in Python

In Python, you can capitalize the first letter of a string using string method str.capitalize().

Capitalizing the first letter of a string using 'str.capitalize()' method

1. Create a variable named str_variable and assign string value "python is a Programming Language." to the variable.

2. Apply the capitalize() method to the string variable str_variable. This method capitalizes the first letter of the string and converts all other characters to lowercase. Store the result in a new variable called cap_variable.

Please take a closer look at the following example:

Example of using capitalize() method

Output of the above example
Python is a programming language.

Live Code Playground

In the following Python code editor, you can practice the above code by creating more string variable and capitalizing them using capitalize method. Click on the 'Execute Code' button to execute the code; the executed output will be displayed in the following Python Code Output frame. Practice until you become comfortable and proficient with your code.

Python logo Python Online Editor
Python Code Output