Python string Method - capitalize()

The capitalize() method in Python strings returns a copy of the string with the first character capitalized and the rest of the characters in lowercase.

The syntax for the capitalize() method is as follows:

ref‮ re‬to:theitroad.com
string.capitalize()

Here, string is the string that we want to capitalize.

Example:

# Defining a string
my_string = "hello, world!"

# Using the capitalize() method
result_string = my_string.capitalize()

print(result_string)   # Output: "Hello, world!"

In the above example, the capitalize() method is used to capitalize the first character of the string my_string. Since the first character is 'h', it is capitalized to 'H'. The rest of the characters are in lowercase. The resulting string is stored in result_string and printed using the print() function. Note that the original string my_string is not modified by the capitalize() method, since strings in Python are immutable.