string capitalize() in Python


The capitalize() method in Python is a built-in string method that returns a copy of the original string with the first character capitalized and the rest of the characters in lowercase.

Syntax:

string.capitalize()

Example:

string = "hello world"
capitalized_string = string.capitalize()
print(capitalized_string)

Output:

Hello world

If the first character of the string is already capitalized, the method will return the original string without any changes.

Example:

string = "Hello world"
capitalized_string = string.capitalize()
print(capitalized_string)

Output:

Hello world

If the string is empty, the method will return an empty string.

Example:

string = ""
capitalized_string = string.capitalize()
print(capitalized_string)

Output:




About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.