Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

capitalize() Function in Python

Last Updated on October 18, 2023 by Ankit Kochar

Capitalization is the act of writing a word with its initial letter in uppercase or capital form, while the subsequent letters are in lowercase or small form. This aspect of writing is significant in written communication as it aids in conveying meaning, enhancing context clarity, and complying with established grammatical and stylistic conventions. Given the importance of capitalization, Python offers a built-in function specifically designed for this task, referred to as "capitalize()".

In this article, we will learn about the capitalize() Function in Python with the help of examples.

What is Capitalize Function in Python?

Python’s "capitalize()" function is a string method that transforms the first character of a string to uppercase while converting all remaining characters to lowercase. This function is an integral part of Python’s core language, signifying that it is readily available for use without the need to import any external modules or packages.

To use the capitalize() function, simply call it on a string object and it will return a new string with the first character capitalized. For example, the code "hello world".capitalize() would return the string "Hello world".

The capitalize() function is useful when working with strings that need to be properly formatted for display or other purposes. It can be used in conjunction with other string methods to manipulate strings in a variety of ways, such as splitting or joining them, searching for substrings, and replacing characters.

Syntax of Capitalize Function in Python

The capitalize() function in Python has a very simple syntax, which is as follows:

stringName.capitalize();

Here, the "stringName" parameter refers to the string that we want to capitalize.

Parameters of Capitalize Function in Python
As shown in Syntax, the capitalize Function in Python does not take any parameters. We just call it on the string we want to capitalize.

Return Type of Capitalize Function in Python
This method returns a new string with the first character capitalized and the rest of the characters in lowercase

Note: It is important to note that this function does not modify the original string. Instead, it only returns a capitalized String with the first character in UpperCase while others in LowerCase.

Examples of Capitalize Function in Python

Here are some examples to demonstrate the usage of capitalize Function in Python.

Example 1: Capitalize Function in Python with All Lowercase Characters in String
Given below is the code for the above-specified example:

str = 'prepbytes'
newStr = str.capitalize()


print('Original String: ', str)
print('New String: ', newStr)

Output:

Original String:  prepbytes
New String:  Prepbytes

Explanation:
In the above code, we have initialized, the string “str” with all characters in LowerCase. When we use the capitalize() Function, it converts the first Character to uppercase, and the rest string is kept in LowerCase and thus giving the output as “Prepbytes”.

Example 2: Capitalize Function in Python with First Character in Uppercase in String
The code for the above example is

str = 'Prepbytes'
newStr = str.capitalize()


print('Orignal String: ', str)
print('New String: ', newStr)

Output:

Orignal String:  Prepbytes
New String:  Prepbytes

Explanation:
We already know that the capitalize () function only transforms the first character of a string to upper case. In this case, the initial character is already the upper case, but the rest of the string is lower case.

As a result, there are no modifications in the new string.

Example 3: Capitalize Function in Python with All Characters in Uppercase in String
This above-mentioned example is demonstrated by the code given below:

str = 'PREPBYTES'
newStr = str.capitalize()


print('Orignal String: ', str)
print('New String: ', newStr)

Output:

Orignal String:  PREPBYTES
New String:  Prepbytes

Explanation:
In the above code, The string ‘str’ was initialized with all upper case letters. Except for the initial character, the captitalize() transformed all uppercase characters to lowercase ones.

As a result, we received the result ‘Prepbytes’.

Example 4: Capitalize Function in Python with Non-Alphabetic First Character in String
Let us understand, what will happen if we try to use the capitalize() function on a String which starts with a Non-Alphabetic Character with the help of code given below.

str = '#PrepBytes'
newStr = str.capitalize()


print('Orignal String: ', str)
print('New String: ', newStr)

Output:

Orignal String:  #PrepBytes
New String:  #prepbytes

Explanation:
In the above code, we initialized the string ‘str’, which has a non-alphabetic first character. It is a ‘hash’ sign in this situation. As a result, when we use the capitalize() function in’str1′. It does not modify the initial character, but it does lowercase the rest of the string.

As a result, we obtained the output ‘#prepbytes’.

Alternatives to Capitalize Function in Python

In Python, there are several alternatives to the capitalize() method that can be used to change the capitalization of strings. Here are a few:

title():
The title() method returns a new string with the first character of each word in the string capitalized. For example

string = "the quick brown fox"
new_string = string.title()
print(new_string)  # Output: The Quick Brown Fox

upper() and lower():
The upper() method returns a new string with all characters in uppercase, while the lower() method returns a new string with all characters in lowercase. For example

string = "The Quick Brown Fox"
new_string_upper = string.upper()
new_string_lower = string.lower()
print(new_string_upper)  # Output: THE QUICK BROWN FOX
print(new_string_lower)  # Output: the quick brown fox

Slicing:
You can also use slicing to capitalize the first letter of a string. For example

string = "the quick brown fox"
new_string = string[0].upper() + string[1:]
print(new_string)  # Output: The quick brown fox

These are just a few alternatives to the capitalize() method in Python. The choice of which one to use depends on the specific requirements of your program.

Conclusion
The capitalize() function in Python is a built-in string method that capitalizes the first character of a string while converting all other characters to lowercase. This function is part of the core Python language and does not require any additional libraries or imports. It is a useful tool for formatting strings according to capitalization conventions and is commonly used in text processing and manipulation tasks.

Frequently Asked Questions (FAQs) related to capitalize() Function in Python

Below are some of the FAQs related to capitalize() Function in Python:

1. Can the capitalize() function handle Unicode characters?
Yes, the capitalize() function can handle Unicode characters as long as they are alphabet characters. It will correctly capitalize the first Unicode alphabet character in the string.

2. Does the capitalize() function modify the original string?
No, the capitalize() function does not modify the original string. Instead, it returns a new string with the desired capitalization.

3. What is the difference between capitalize() and title() functions in Python?
The capitalize() function capitalizes only the first character of the string, while the title() function capitalizes the first character of each word in the string.

4. Can I use the capitalize() function on an empty string?
Yes, you can use the capitalize() function on an empty string, and it will return an empty string as well.

5. Are there any performance considerations when using the capitalize() function on large strings?
The capitalize() function is generally efficient and performs well, even on large strings, as it only processes each character once.

6. In which Python versions is the capitalize() function available?
The capitalize() function is available in all Python versions, including Python 2.x and Python 3.x, as it is a fundamental string method in the Python standard library.

Leave a Reply

Your email address will not be published. Required fields are marked *