Menu Close

Python string capitalize() method

String capitalize() Method

In this article, you will learn about the python string capitalize() method. Python string capitalize method is used to work with string. Here you will learn about the python capitalize() method which is used to convert the first character of a string in the upper case.

To learn all about Python string, Click Here

Python String capitalize() method:

Python string capitalize method is used to change the first character of the string
into uppercase without changing the whole string.

Syntax:

The syntax of Python capitalize() method is:

list.capitalize()

Return Type:

The String capitalize function returns the string with the first letter capitalize and all other characters are lowercased.

Parameter:

The Python string capitalize method does not take any parameter.

Let’s see an example for using Python capitalize() string method.

Python string capitalize() method example:

Example

str = "programming funda"
print("Old string:- ", str)

str2 = str.capitalize()
print("New string:- ",str2)

Output

Old string:-  programming funda
New string:-  Programming funda

Example 2:

If the first character of the string is already in uppercase, then the python capitalize () method what will do.

str = "Python programming"
print("Old string:- ", str)

str2 = str.capitalize()
print("New string:- ",str2)

Output

Old string:-  Python programming
New string:-  Python programming

Example 3:- None alphabetic first character:

If the first character of the string is not an alphabet, Then the Python capitalize() string method returns the same string.

str = "12Python programming"
print("Old string:- ", str)

str2 = str.capitalize()
print("New string:- ",str2)

Output

Old string:-  12Python programming
New string:-  12python programming

Conclusion:

In this article, You have learned what is Python string capitalize() method and how to use the string capitalize function to convert the first character of a string in upper case.

I hope this article will help you, if you like this article, please comment and share with your friends who want to learn python from scratch to advanced.

For More Reference:- Click Here

Next Article

Related Posts

Leave a Reply