This is a tutorial of the Python String capitalize() method. Learn to capitalize the first letter of a string in python using this method with examples.
capitalize() when applied on a given string returns a new string after capitalizing the first letter and if the given string contains more than one letter, it makes all of the other letters of the string small in the returned string.
Given below is the syntax of this method.
capitalizedString = givenString.capitalize()
This method does not take any arguments.
capitalize() Return Value
It returns the string on which the
capitalize() method is applied but after converting the first letter into UpperCase and if any exists, it converts all of the other letters of the string into lowercase in the returned string.
Note. No modifications are being made to the original string.
Given below are the two examples demonstrating the usage of the Python String
Example 1. Capitalizing Strings in Python
In this example, we’ve defined a string into the variable
givenString and then on this variable, we’ve simply applied the
capitalize() method to get the new capitalized string.
givenString = "amandeep Singh" #Converting first letter of the string into uppercase and all of the other letters into lowercase using the string method capitalize() capitalizedString = givenString.capitalize() print("Given String:", givenString) print("Capitalized String:", capitalizedString)
Given String: amandeep Singh Capitalized String: Amandeep singh
Example 2. Working of
capitalize() method, if the first character of the String is Non-Alphabetical
If the first letter of the given string on which we’re applying this method is non-alphabetical, then this function keeps the first letter unchanged but it converts all of the other letters of the string into lowercase if any exists.
Here in the following example, the character or symbol
$ is non-alphabetical, hence it remains unchanged in the returned string while all of the other alphabetical characters of the string are converted into the lowercase form.
words = "$100 is The saVings FOR this month." capitalizedWords = words.capitalize() print("Original String: ", words) print("Capitalized String: ", capitalizedWords)
Original String: $100 is The saVings FOR this month. Capitalized String: $100 is the savings for this month.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!