Python String capitalize() Method



The Python String capitalize() method is used to capitalize the current string. It simply returns a string with the very first letter capitalized, that is in upper case and the remaining characters of that string are converted to lower case letters.

If the first letter of the input string is a non-alphabet or if it is already a capital letter, then there will be no effect in the output i.e. the original string is not modified.

We will be learning more details about the python string capitalize() method in this chapter.

Syntax

The following is the syntax for the python string capitalize() method.

str.capitalize()

Parameters

This method does not accept any parameters.

Return Value

This method returns the capitalized string as its output.

Example

Following is an example of the python string capitalize() function. In here, we are trying to capitalize the first letter of the string "tutorialspoint".

str = "tutorialspoint"
output=str.capitalize()
print("The resultant string is:", output)

On executing the above program, the following output is generated -

The resultant string is: Tutorialspoint.

Example

If the first letter of the input string is already a capital letter, then the capitalize() function returns the current string without any changes.

In the following example, we are creating a string with value "Tutorialspoint" as the first character of it is already a capital leter, if we invoke the capitalize() function on this string, it returns the current string without any changes.

str = "Hii! welcome to tutorialspoint."
output=str.capitalize()
print("The resultant string is:", output))

The following is the output obtained by executing the above program -

The resultant string is: Hii! welcome to tutorialspoint.

Example

If the first letter of the input string is not an alphabet, this function returns the original string.

In the following example, we are creating a string value with "$" as the first character and called the capitalize() function using this string. Since the starting letter is not an alphabet this function returns the current string without any changes.

str = "$Hii! welcome to tutorialspoint."
output=str.capitalize()
print("The resultant string is:", output)

The following output is obtained by executing the above program -

The resultant string is: $hii! welcome to tutorialspoint.

Example

The python string capitalize() function does not modify the original string. So, if you print it, the original string is printed without any modifications.

str = "hii! welcome to tutorialspoint."
output=str.capitalize()
print("The string after applying the capitalize() function is:", output)
print("The original string is:", str)

The above program, on executing, displays the following output -

The string after calling the capitalize() function: Hii! welcome to tutorialspoint.
The original string is: hii! welcome to tutorialspoint.

Example

The python string capitalize() function only capitalizes the very first letter. If the remaining characters in the string contain capital letters, they are all changed to small letters.

   str = "hii! Welcome to TUTORIALSPOINT."
   output=str.capitalize()
   print("The string after applying the capitalize() function is:", output)

The output of the above program is displayed as follows -

The string after calling the capitalize() function: Hii! welcome to tutorialspoint.
The original string is: hii! welcome to tutorialspoint.
python_strings.htm
Advertisements