Python capitalize

Python capitalize function is used to convert the first character into Uppercase and the remaining characters to Lowercase and returns a new string. This section discusses how to write a capitalize Function with an example.

It keeps the Non-letters unchanged. The syntax of the Python capitalize function is

String_Value.capitalize()

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 Python 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 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)
Python capitalize Example