In Python, there are built-in functions for practically any action that can be performed on a string. There may be occasions when the user wishes to convert a string from lowercase to uppercase. For example, if someone writes his name in small letters, but we wish to convert the initial letter of the name to uppercase, which is a worldwide standard for writing the first word. To overcome this problem, we utilize the python capitalize() method when we need the first character of every given string in uppercase or capital letter and the rest of the characters in lowercase or tiny letter.
Definition
- Python capitalize() is a string method used to convert the first character of the input string to uppercase, and rest to lowercase, if any.
- The Python capitalize() inbuilt function returns a copy of the string where the first letter of the string is converted into uppercase while the rest all the characters are kept lowercase.
Python capitalize() Syntax
The syntax followed by Python capitalize() method is as follows:
string.capitalize()
capitalize() Parameters
The capitalize() function does not accept any parameters.
Return Value from capitalize()
The capitalize() method produces a string in which the first letter is capitalized and all other characters are lowercased. It makes no changes to the original string.
Note – If the first character of the input string is already uppercase, then the capitalize() function will return the original string only with other characters converted to lowercase.
Example 1: Capitalize a Sentence
Because the method only capitalizes the first letter of the string, only the first letter of the sentence is capitalized in the preceding example. Even if they were capital in the original string, the remaining characters are lowercased.
Example
# Python program to illustrate capitalize()
my_str = 'good morning'
print('Old String: ', my_str)
print('Capitalized String:', my_str.capitalize())
print('')
my_str = 'PYTHON PROGRAM'
print('Old String: ', my_str)
print('Capitalized String:', my_str.capitalize())
Output
Old String:Â good morning
Capitalized String: Good morning
Old String:Â PYTHON PROGRAM
Capitalized String: Python program
Example 2: Non-alphabetic First Character
If the initial letter isn’t alphabetical, the procedure doesn’t convert it to a capital letter, but it does change all subsequent letters to lowercase.
Example
# Python program to illustrate capitalize()
my_str = '#$ Hello WORld'
print('Old String: ', my_str)
print('Capitalized String:', my_str.capitalize())
print('')
my_str = '123CarS'
print('Old String: ', my_str)
print('Capitalized String:', my_str.capitalize())
Output
Old String:Â #$ Hello WORld
Capitalized String: #$ hello world
Old String:Â 123CarS
Capitalized String: 123cars
Example 3: capitalize() with other Object types
When we apply the capitalize() function on objects of different data types, such as a number or a None value, we get an error. This is because capitalize() function is specifically designed for string datatypes only. It does not work if any other data type is passed to it. Let’s look at both cases and the results:
Example
my_str = 100
print('Old String: ', my_str)
print('Capitalized String:', my_str.capitalize())
Output
Old String:Â 100
Traceback (most recent call last):
File "", line 3, in
AttributeError: 'int' object has no attribute 'capitalize'
Example
my_str = None
print('Old String: ', my_str)
print('Capitalized String:', my_str.capitalize())
Output
Old String:Â None
Traceback (most recent call last):
File "", line 3, in
AttributeError: 'NoneType' object has no attribute 'capitalize'
Frequently Asked Questions
Q1. What does capitalize() do in Python?
There may be occasions when the user wishes to convert a string from lowercase to uppercase. To overcome this problem, we utilize the python capitalize() method when we need the first character of every given string in uppercase or capital letter and the rest of the characters in lowercase or tiny letter.
The Python capitalize() inbuilt function returns a copy of the string where the first letter of the string is converted into uppercase while the rest all the characters are kept lowercase.
Example
txt = 'i love cars'
print(txt.capitalize())
txt = 'diNNer Is ReadY'
print(txt.capitalize())
Output
I love cars
Dinner is ready
Q2. How do you uppercase a word in Python?
In Python, there are 2 string methods used to convert characters to uppercase. They are:
- Python upper() – The Python upper() method returns a string that has all lowercase characters converted to uppercase characters.
- Python capitalize() – The capitalize() function in Python turns the first character of a string to an uppercase letter and lowercases any remaining characters.
Example
txt = 'my exams went superb'
print(txt.upper())
txt = 'i am excited for this trip'
print(txt.capitalize())
Output
MY EXAMS WENT SUPERB
I am excited for this trip
Q3. What is the difference between upper(), title() and capitalize() in Python?
The Python upper() method returns a string that has all lowercase characters converted to uppercase characters.
The title() method is a Python String Method that is used to change the initial character of each word in the string to Uppercase and the remaining characters in the string to Lowercase and returns a new string.
The capitalize() function in Python changes the first character of a string to a capital letter. If the first character in the string is a capital, it returns the original string.
Example
txt = 'dancing is my passion'
print(txt.upper())
txt = 'did you eat a pizza?'
print(txt.title())
txt = 'i am so nervous'
print(txt.capitalize())
Output
DANCING IS MY PASSION
Did You Eat A Pizza?
I am so nervous
Leave a Reply