Python String capitalize() Method
In Python, strings (sequences of characters) are utilized for various tasks such as getting or extracting information, displaying information, etc. While dealing with strings, we may sometimes need to arrange the strings in a specific way like capitalizing the first character or capitalizing all the characters in upper or lowercase. The “string.capitalize()” method of Python retrieves the new string with the first character in upper case while the other is in lowercase.
This Python tutorial represents an in-depth overview of the “string.capitalize()” method utilizing numerous examples. Let’s start with the following contents:
What is the String capitalize() Method in Python?
Alternative Methods to Capitalize a String in Python
What is the String capitalize() Method in Python?
In Python, the “string.capitalize()” method is utilized to convert the first letter/alphabet of the given string to the upper case letter/alphabet and other string letters to the lower case alphabet.
Syntax
Parameters
In the above syntax, the “string” specifies the particular string that needs to be capitalized. Besides that, this method does not take any value.
Return Value
The retrieved value is a string with the foremost/first character as an uppercase letter. It will retrieve the same string if the input string is non-alphabet or the first character is already uppercase.
Example 1: Capitalize the Given String Using the “string.capitalize()” Method
The initial thing we perform is to initialize our desired string. Next, the original string is displayed to the output. Lastly, the “string.capitalize()” method retrieves the new string with the first letter upper case and the other letter lowercase.
print(string_value, '\n')
print(string_value.capitalize())
The above code displayed the original and modified string:
Example 2: Retrieving Value of the Given String Using the “string.capitalize()” Method
The “string.capitalize()” method retrieves a new string rather than modification of the given string. To verify this, we will give an example. In this example, we store the original string value to the “string” variable, and after applying capitalization method value to the “string_1” variable. Both retrieve values printed on the screen, which confirms that the “string.capitalize()” method does not modify/capitalize the given string.
string_1 = string.capitalize()
print("New String: ", string_1)
print("\nOriginal String: ", string)
The below snippet shows the original string and the modified string:
Example 3: Capitalizing the String With Non-Alphabet Character Using the “string.capitalize()” Method
As we know from the previous section, the “string.capitalize()” does not change the non-alphabetic character. Here, in this example, the first non-alphabetic character “10” remains unchanged in the new retrieve string, while all other letters/characters are converted into lowercase letters.
print(string_value, '\n')
print(string_value.capitalize())
The return string and the input string are shown below:
The “string.capitalize()” method also behaves similarly if the first character is any symbol such as @, #, $, etc. Take this code for instance:
print(string_value, '\n')
print(string_value.capitalize())
The above code shows the following snippet:
Alternative Methods to Capitalize a String in Python
In Python, we can employ various methods to capitalize the given string. Some of the methods include “upper()”, “lower()” and “title()” methods. All these methods can convert the input string to uppercase, lower case, or first character upper and other lower. Let’s further overview this by the below code:
Example 1: Using the “title()” Method to Capitalize a Python String
The “title()” method is very similar to the “string.capitalize()” method. The only difference is that it converts the first letter of every word in the sentence to uppercase, while the “string.capitalize()” method converts only the first character of the sentence. All other letters/characters in the new string are retrieved by the string.capitalize() method is a lowercase letter. Take this as an example:
print(string_value, '\n')
print(string_value.title())
The above code retrieves the new modified string:
Example 2: Using the “upper()” Method to Capitalize a Python String
The “upper()” method can also be used to capitalize the Python string. This method capitalizes all the alphabet of the given string to the upper-case letter.
print(string_value, '\n')
print(string_value.upper())
Here is the output:
We can also use the “lower()” method of Python. It can convert all the given string values to the lowercase letter.
print(string_value, '\n')
print(string_value.lower())
The following snippet retrieved the result of the “string.lower()” method:
Conclusion
The “string.capitalize()” method is utilized in Python to capitalize the first letter of the string and to convert all other characters to lowercase letters. It does not modify/change the original string, rather it will retrieve a new string. This method does not apply to non-alphabet characters or symbols of the given string value. There are also other ways to capitalize a string in Python such as upper(), lower(), and title() methods. This detailed tutorial provided you with an in-depth analysis of the Python “string.capitalize()” method via several examples.
Source: linuxhint.com