Python String capitalize()

Python String‘s capitalize method is used to convert first character of String to uppercase.


Syntax

Here s is object of the String.


Return

It returns new String with first character of String as uppercase.


Example

Let’s see a very simple example:

s='hello world' sCap=s.capitalize() print("Old String:",s) print("New String:",sCap)

Output:

Old String: hello world
New String: Hello world

In case of non alphanumeric String, it won’t change anything.
Let’s see a very simple example:

s='2 is the number' sCap=s.capitalize() print("Old String:",s) print("New String:",sCap)

Output:

Old String: 2 is the number
New String: 2 is the number

That’s all about Python String capitalize() method.

Leave a Reply

Your email address will not be published. Required fields are marked *