Python capitalize

Python capitalize function is used to convert the first character into Uppercase and remaining characters to Lowercase and return a new string. In this section, we discuss how to write a capitalize Function in Python Programming with an example.

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

String_Value.capitalize()

String_Value: Please select the valid String variable, or you can use the String directly.

Python capitalize method Example

The following set of examples help you understand the capitalize Function.

# Python Capitalize Method Example

Str1 = 'learn PYTHON AT tutOrial gateWay'

Str2 = Str1.capitalize()
print('First Output after Capitalize() method is = ', Str2)

# Observe the Original String
print('Converted String is = ', Str1.capitalize())
print('Original String is = ', Str1)

# Performing Capitalize() function directly
Str3 = 'pYTHON proGramminG'.capitalize()
print('Second Output after Capitalize() method is = ', Str3)

Str4 = 'pyTHon 1234 tuTorIal'.capitalize()
print('Third Output after Capitalize() method is = ', Str4)

Str5 = '12345'.capitalize()
print('Fourth Output after Capitalize() method is = ', Str5)
Python Capitalize

The following statement converts the above string into the capitalizing case using capitalize function and prints the output.

Str2 = Str1.capitalize()
print('First Output after Capitalize() method is = ', Str2)

The Python capitalize function returns the output in a new string instead of altering the original string.

print('Converted String is = ', Str1.capitalize())
print('Original String is = ', Str1)

From the above 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 string capitalize function converts the First letter into Uppercase and convert the following words into lowercase. But it leaves other values unchanged. Within the following statements, we used the numeric values along with letters, and you can see the above String Method output for further reference.

Str4 = 'pyTHon 1234 tuTorIal'.capitalize()
print('Third Output after Capitalize() method is = ', Str4)

Str5 = '12345'.capitalize()
print('Fourth Output after Capitalize() method is = ', Str5)