How to Capitalize First Character of String in Python?

Python String capitalize()

To convert first character of a String to Capital Letter in Python, use String.capitalize() function.

Example 1: First Character is a Lower Case

In the following Python Example, the first character of given String is lower case alphabet. We will use String.capitalize() function to capitalize the first character.

str = 'welome to python examples'

str1 = str.capitalize()

print('Original String    :',str)
print('Capitalized String :',str1)
Run

Output

Original String    : welome to python examples
Capitalized String : Welome to python examples
Run

Example 2: First Character is an Upper Case

If the first character is upper case alphabet, then applying String.capitalize() would not make any change in the resulting string.

str = 'Welome to python examples'

str1 = str.capitalize()

print('Original String    :',str)
print('Capitalized String :',str1)
Run

Output

Original String    : Welome to python examples
Capitalized String : Welome to python examples
Run

Example 3: First Character is a Numeric Digit

capitalize() function converts only lower case alphabets to upper case. So, it does not effect a string, if the first character is a numeric digit.

str = '142 welome to python examples'

str1 = str.capitalize()

print('Original String    :',str)
print('Capitalized String :',str1)
Run

Output

Original String    : 142 welome to python examples
Capitalized String : 142 welome to python examples
Run

The same explanation holds if the first character is a space, special symbol, etc.