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

Capitalization is defined as the process of writing a word with its first letter in uppercase or capital letter form, and the remaining letters in lowercase or small letter form. Capitalization plays an important role in written communication, helping to convey meaning, clarify context, and adhere to established conventions of grammar and style. Since Capitalization holds this much importance, Python has a Built-In Function for this purpose known as capitalize().

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

Capitalize Function

The capitalize() function in Python is a string method that capitalizes the first character of a string and converts all other characters to lowercase. It is a built-in method in Python, which means that it is a part of the core language and does not require any additional modules or packages to be imported.

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.

Summary
In Summary, the capitalize() function is a built-in string method in Python that capitalizes the first character of a string and makes all the other characters lowercase. It does not modify the original string but returns a new string with the capitalization applied. The syntax for the function is “stringName.capitalize()”, and it does not take any parameters. The function is case-sensitive and only capitalizes the first alphabetical character in the string. To capitalize the first letter of each word in a string, the title() function can be used instead. The capitalize() function can be used on strings that contain numbers or special characters, but it will only capitalize the first alphabetical character in the string.

Frequently Asked Questions (FAQs)

Q1: What is the return value of the capitalize() function?
A: The capitalize() function returns a new string with the first character capitalized and all the other characters in lowercase.

Q2: Is the capitalize() function case-sensitive?
A: Yes, the capitalize() function is case-sensitive. It only capitalizes the first character of the string and makes all other characters lowercase.

Q3: Does the capitalize() function modify the original string?
A: No, the capitalize() function does not modify the original string. It returns a new string with the first character capitalized and all the other characters in lowercase.

Q4: How can I capitalize the first letter of each word in a string?
A: You can use the title() function instead of capitalize() to capitalize the first letter of each word in a string. The syntax for the title() is

stringName.title()

Q5: Can I use the capitalize() function on a string with numbers or special characters?
A: Yes, you can use the capitalize() function on a string with numbers or special characters. The function will only capitalize the first alphabetical character in the string and leave the rest of the characters unchanged.

Leave a Reply

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