Contents
Python String capitalize()
To convert first character of a String to Capital Letter in Python, use String.capitalize()
function.
Examples
1. Capitalise given string when first character is a lowercase
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 Code Online Output
Original String : welome to python examples
Capitalized String : Welome to python examples
Run Code Online 2. Capitalise given string when first character is an uppercase
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 Code Online Output
Original String : Welome to python examples
Capitalized String : Welome to python examples
Run Code Online 3. Capitalise given string when 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 Code Online Output
Original String : 142 welome to python examples
Capitalized String : 142 welome to python examples
Run Code Online The same explanation holds if the first character is a space, special symbol, etc.
Summary
In this tutorial of Python Examples, we learned how to capitalise a given string in Python language with the help of well detailed example programs.