The Python capitalize function is used to convert the first character to uppercase and the remaining characters to lowercase, returning a new string. It keeps the Non-letters unchanged. This section discusses how to write the capitalize() function with an example.
Python capitalize syntax
The syntax of the capitalize function is as shown below.
String_Value.capitalize()
Parameters: It does not accept any parameter value.
Return Value: The capitalize() function returns a new string with the first character in uppercase and the remaining characters in lowercase. Remember, it does not modify the original string.
Python capitalize method Example
The following set of examples helps you understand the capitalize Function. The first Str2 statement converts the above string into the capitalizing case using and prints the output.
It returns the output in a new string instead of altering the original string. We use the next two print statements to show the difference.
From the below screenshot, you can observe that the original string Str1 is unchanged. If you want to change the original String, then you can write the following Python statement.
Str1 = Str1.capitalize()
The capitalize function converts the First letter into Uppercase and the following words into lowercase. But leaves other values unchanged. Within the following Str4 and Str5 statements, we used the numeric values along with letters, and you can see the above string method output for further reference.
Str1 = 'learn PYTHON AT tutOrial gateWay'
Str2 = Str1.capitalize()
print('First Output is = ', Str2)
# Observe the Original String
print('Converted String is = ', Str1.capitalize())
print('Original String is = ', Str1)
Str3 = 'pYTHON proGramminG'.capitalize()
print('Second Output is = ', Str3)
Str4 = 'pyTHon 1234 tuTorIal'.capitalize()
print('Third Output is = ', Str4)
Str5 = '12345'.capitalize()
print('Fourth Output is = ', Str5)

Using capitalize() when the first character is a number
As we all know, the capitalize function skips the non-alphabetic character and performs its operation. If we use the capitalize() function on a string with a first character that is a number, it retains the number unchanged and converts the remaining characters to lowercase.
s = "10th YEAR CeleBRATION"
print(s.capitalize())
10th year celebration
Python capitalize the first character of each word in a string
In the following example, we declared a string with seven words. Our task is to capitalize the first letter in each word, and the remaining characters should be in lowercase.
Here, the split() function splits the string, and we used a list comprehension to iterate over the words in a string. On each iteration, apply the capitalize() function, meaning the function will apply to each word. Remember, we can also use the built-in title() function for the same result.
s1 = "welcome to the UNITED states OF AMERICA."
capWords = [word.capitalize() for word in s1.split()]
s2 = ' '.join(capWords)
print("Before: ", s1)
print("After : ",s2)
Before: welcome to the UNITED states OF AMERICA.
After : Welcome To The United States Of America.
Python capitalize first letter of every word in lists
We can use the built-in string capitalize function to set the first character of each list item to uppercase and convert the remaining characters to lowercase. Imagine if we have a string list with mixed characters, we can use the capitalize() or title() function to normalize the strings.
In the following example, we declared a list of fruits with different names. Next, a list comprehension iterates over the list of fruits and applies the capitalize() function on each list item (fruit).
fruits=['apple','grapes','orange', 'kiwi', 'mango']
New_fruits = [i.capitalize() for i in fruits]
print(New_fruits)
['Apple', 'Grapes', 'Orange', 'Kiwi', 'Mango']
What is the difference between upper and capitalize in Python?
The built-in upper() function converts all characters in a given string to uppercase letters. On the other hand, the capitalize() function converts the first character in a given string to uppercase, and the remaining characters are converted to lowercase letters.
If you need to convert the whole string to uppercase, use the upper() function. If your preference is to convert the first character in a string to uppercase, prefer the capitalize() function.
s1 = "Hello WORld"
s2= s1.upper()
print(s2)
s3 = s1.capitalize()
print(s3)
HELLO WORLD
Hello world