String capitalize() Method in Python
Python String capitalize() method returns a copy of the original string and converts the first character of the string to a capital (uppercase) letter while making all other characters in the string lowercase letters.
Example
# initializing a string
name = "draco malfoy"
#using capitalize function
print(name.capitalize())
Output
Draco malfoy
Definition of capitalize() Function
The capitalize() function in Python is used to capitalize the first letter of a string. It turns the rest of all letters to lowercase.
If the first letter is not an alphabet, capitalize() function will leave it as it is and converts rest of the characters to small case.
capitalize() Method Syntax
string_name.capitalize()
Parameter
The capitalize() function does not take any parameter.
Return
The capitalize() function returns a string with the first character in the capital.
capitalize() Method Examples
Let’s look at some capitalize in Python examples, to understand the function better.
name = "geeks FOR geeks"
print(name.capitalize())
Output:
Geeks for geeksExample 1: capitalize() only capitalizes the first letter of a string and lowers all the remaining characters
string = "roses are red"
print("Original string:", string)
print("After using capitalzie:", string.capitalize())
Output:
Original string: roses are red
After using capitalize: Roses are red
Example 2: Python String capitalize () Method Doesn’t Modify the Original String
Python String capitalize () Method creates and returns a copy of the original string after modifications.
string = "geeks for geeks"
string_2 = string.capitalize()
print("New string after using capitalize():", string_2)
print("Original string:", string)
Output
New string after using capitalize(): Geeks for geeks Original string: geeks for geeks
We have covered how to capitalize a string in Python using the capitalize () function. It is useful for data formatting.
Example 3: Python String capitalize () Method leave as it is if first letter is not an alphabet
string1 = "$this is an Example StRing"
string2 = "this is an Example StRing"
# using capitalize function
print(string1.capitalize()) # Output : $this is an example string
print(string2.capitalize()) # Output : This is an example string
Output
$this is an example string This is an example string

