Python String capitalize()


Python String capitalize() method

Python String capitalize() method is used to capitalize the first character in the string.

Consider that given string is in x.

x = "hello world"

Then the return value of x.capitalize() is

"Hello world"

Syntax of capitalize() method

The syntax of String capitalize() method in Python is given below.

string.capitalize()

The capitalize() method takes no parameters.

capitalize() method returns a new string created using the given string and capitalizing the first character in the string.

The original string is not modified in value, and remains the same.

Examples

1. Capitalize given Python string

In the following program, we take a string in variable x, and capitalize the string using String capitalize() method. We shall store the capitalize() returned value in result variable. And then print both the original string x and capitalized string result.

Python Program

x = "hello world"
result = x.capitalize()
print(f"Given string       : {x}")
print(f"Capitalized string : {result}")

Output

Given string       : hello world
Capitalized string : Hello world

Summary

In this tutorial of Python String Methods, we learned about String capitalize() method, its syntax, and examples.