English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Função do Python

Tipos de Dados do Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python string capitalize() usage and examples

Python string methods

In Python, the capitalize() method converts the first character of a string to uppercase and converts all other characters (if any) to lowercase.

The syntax of capitalize():

string.capitalize()

capitalize() parameters

capitalize() function without any parameters.

The value returned by capitalize()

The capitalize() function returns a string with the first letter capitalized and all other characters lowercase. It does not modify the original string.

Example1: Capitalize the first letter of a sentence

string = "pt.oldtoolbag.com"
capitalized_string = string.capitalize()
print('Old string: ', string)
print('Capitalized string:', capitalized_string)

When running the program, the output is:

Old string: pt.oldtoolbag.com
Capitalized string: pt.oldtoolbag.com

Example2: The first character is not a letter

string = "+ is an operator."
new_string = string.capitalize()
print('Original string:', string)
print('New string:', new_string)

When running the program, the output is:

Original string: + is an operator.
New string: + is an operator.

Python string methods