String capitalize()

Python String capitalize() method converts the first character in the string to upper case.

In this tutorial, we will learn the syntax and examples for capitalize() method of String class.

Syntax

The syntax of String capitalize() method in Python is

</>
Copy
str.capitalize()

Examples

Capitalize a String

In this example, we will take a string, and capitalize the first character of this string using str.capitalize() method.

Example.py

</>
Copy
x = 'hello world!'
result = x.capitalize()
print(result)

Output

Hello world!

Capitalize a String that is Already Capitalized

If the given string is already capitalized, then str.capitalize() does nothing.

Example.py

</>
Copy
x = 'Hello world!'
result = x.capitalize()
print(result)

Output

Hello world!

Conclusion

In this Python Tutorial, we learned how to capitalize the first character in string using String method – capitalize().