Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The capitalize() method in Python transforms the first character of a string to uppercase, while converting the rest of the characters to lowercase.
Our today's topic for this blog is Capitalize() in Python. Here we will discuss all the features, along with examples. Let's start.
What is Capitalize in Python?
The Python String capitalize() method is used to capitalize the given string. It returns a string with the first letter capitalized, that is, in upper case, and the other characters of that string are converted to lower case letters.
Python String capitalize() Method Syntax
The syntax of Capitalize() in Python is as follows:
string.capitalize()
Here,
The string is the given input.
And the capitalize() method will change the input's first character into upper case and the rest into lower case.
Parameters of Capitalize()
In Python, the capitalize() method for strings does not take any parameters. It is a built-in method that operates directly on a string object
Return Value
The capitalize() method in Python always returns a string in which the first letter is in uppercase, and all other characters are in lowercase.
Examples of Using the capitalize() Method in Python
Example 1:Capitalizing the First Letter of a Sentence
Python
Python
sentence = "this is an example" capitalized_sentence = sentence.capitalize() print(capitalized_sentence)
You can also try this code with Online Python Compiler
Other than this, we can use many more methods to capitalize in python. Let's have a look at those methods too.
slicing() and upper()
In this approach, the first letter of the string was extracted using the slicing method. After that, we changed it to uppercase by using the string manipulation method upper(). This gives you access to the first letter of each word in the string, even those that are separated by spaces.
Sample Example:
Python
Python
string = "codingninjas" print("Before capitalizing first letter:") print(string)
result = string[0].upper() + string[1:] print("After capitalizing first letter:") print(result)
You can also try this code with Online Python Compiler
We can capitalize the first letter of each word and alter the rest letters to lowercase by using the string.title() method, which gives us the desired output. This method can also be used to format strings in HTML, JavaScript, and other programming languages.
Sample Example:
Python
Python
string = "codingninjas" print("Before capitalizing first letter:") print(string)
print("After capitalizing first letter:") print(str.title(string))
You can also try this code with Online Python Compiler
A Python function called capwords() changes every word's first letter from lowercase to uppercase and vice versa. The function receives a string as a parameter value and outputs a string with the first letter capitalized.
Sample Example:
Python
Python
import string
myString = "codingninjas" print("Before capitalizing first letter:") print(myString)
print("After capitalizing first letter:") result = string.capwords(myString) print(result)
You can also try this code with Online Python Compiler
Regex is known as a regular expression in Python. This special sequence of letters helps match or find the other strings. Regex allows you to capitalize each word by searching for the first character of the word. Using this method, Python's whitespace between words is left alone, and only the first character of each word is capitalized.
We can use the join() method as an alternative to capitalize() in Python. While capitalize() capitalizes the first character of the string. We can also use the str.join() method to capitalize the first character of each word in a string. Let us discuss it with the help of an example.
Python
Python
str = "i love coding ninjas." capitalized_words = [word.capitalize() for word in str.split()] capitalized_string = ' '.join(capitalized_words) print("String before performing title() method: ", str) print("String after performing title() method: ",capitalized_string)
You can also try this code with Online Python Compiler
The capitalize() method helps implement the task more easily.
It saves a lot of time in the case of operations and several tasks.
The capitalize() method help in data security.
The capitalize() method helps hide the execution details.
Disadvantage of Capitalize()
There is a disadvantage of Capitalize() in Python. That says you can not use any other special characters or digits as the first character of the input string.
Now, you must be curious about what will happen if we do not follow the rule above. Let's check this with the help of an example.
Python
Python
# Using special character string1 = "$coding ninjas"
capitalized_string = string1.capitalize()
print("Using special character:", capitalized_string)
print("----------------------------------------")
# Using numeric character string2 = "1coding ninjas"
So, in the above example, we have seen that if the first character of the given string is either any special character or any digit, then the method capitalize() will not work. As a result, it will return the same output as the given input.
To input uppercase in Python, use the upper() method, e.g., my_string.upper().
How do you capitalize in Python format?
To capitalize in Python, apply the capitalize() method, e.g., my_string.capitalize().
Do you capitalize Python coding?
Capitalization in Python coding style follows conventions, like PEP 8, but it doesn't impact code execution.
What is the Caps function in Python?
We can use upper() or capitalize() for uppercase transformations.
Conclusion
In this article, we have discussed capitalize() in Python. The capitalize() method in Python serves as a powerful tool for manipulating strings. It elegantly transforms the first character to uppercase, it helps in enhancing the text readability.
For learning more, check out some of our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
You can also consider our paid courses such asDSA in Python to give your career an edge over others!