What is Capitalize in Python?
The purpose of this blog is to explain the concept of Capitalize in Python. We have explained this concept with the help of relevant examples. Let’s explore.
The capitalize() in Python is the method that changes a string’s first character to uppercase while converting the rest to lowercase. The method returns the modified string without altering the original input string.
In this article, we will discuss the capitalize() function and understand its use with the help of examples. We will be covering the following sections:
- Introduction to capitalize() Method in Python
- Examples of Using the capitalize() Method in Python
- Alternative Methods to Capitalize in Python
Explore Online Python Courses
Introduction to capitalize() Method in Python
The capitalize() method is a built-in string function in Python that is used to modify the case of the characters in a string. Specifically, it changes the first character of a string to uppercase and converts all other characters to lowercase. This method is significant when you want to standardize the formatting of strings, such as when dealing with user inputs or data from external sources.
Remember that the capitalize() method returns a new string with the modified case without changing the original string. Therefore, if you want to modify the original string permanently, you need to reassign the modified string back to the original variable.
Syntax of capitalize()
The syntax of the capitalize() function in Python is given as follows:
string.capitalize()
Parameters of capitalize()
Where the string is the string to be capitalized. The capitalize() function doesn’t require any arguments to be passed. Instead, we call it on the string or variable we want to change.
Return Value of capitalize()
The function returns a string, which is obtained by taking an input string and converting its first character to uppercase and the rest of the characters to lowercase. This modified string is then returned as the result of the function.
Remember that the capitalize() method returns a newly updated string with the modified case without altering the original string. Therefore, if you want to modify the original string permanently, you need to reassign the modified string back to the original variable.
Examples of Using the capitalize() Method in Python
Example 1: Capitalizing the First Letter of a Sentence
# define a string variablemy_string = "this is an example string."
# apply capitalize() methodmodified_string = my_string.capitalize()
# print the modified stringprint(modified_string)
Output:
This is an example string.
In this example, we first define a string variable my_string with the value “this is an example string.”. Using dot notation, we apply the capitalize() method to this string and store the result in a new variable modified_string. Finally, we print the modified string with the first letter capitalized.
Let’s look at more examples:
Example 2: Capitalizing the first letter of a mixed-case text
str1 = "hEy theRe, WeLcOme hOmE!"print (str1.capitalize())
Output:
Hey there, welcome home!
Example 3: Using capitalize() when the first character is not an alphabet
myStr = "#newComment"print (myStr.capitalize())
Output:
#newcomment
This example is an exceptional case. Here, the first character is a “#” which can’t be converted to uppercase. However, if present, any other alphabets in the string will still be converted to lowercase as usual.
Similarly, if the string starts with a number, space or a newline character, the first character will not be converted to uppercase.
Best-suited Python courses for you
Learn Python with these high-rated online courses
Alternative Methods to Capitalize in Python
There are several alternatives to the capitalize() method in Python that you can use depending on your specific needs. Here are a few examples:
upper() and lower() methods
The upper() method can convert all characters of a string to uppercase, while the lower() method can convert all characters of a string to lowercase. Using the upper() method will capitalize the first character in a string, and the lower() method will convert the rest to lowercase.
Example:
my_string = "come here!"modified_string = my_string[0].upper() + my_string[1:].lower()print(modified_string)
Output:
Come here!
title() method
The title() method can be used to capitalize the first letter of each word in a string.
Example:
new_string = "no country for old men"modified_string = new_string.title()print(modified_string)
Output:
No Country For Old Men
swapcase() method
With the swapcase() method, you can swap the case of all characters in a string, i.e., uppercase characters will become lowercase and vice versa.
Example:
my_string = "mOTH sMOKE"modified_string = my_string.swapcase()print(modified_string)
Output:
Moth Smoke
These are just a few examples of alternatives to Python’s capitalize() method. Other methods may better suit your needs depending on your specific use case.
Endnotes
This article was helpful for you in understanding how and why the capitalize() method is used in Python. You can explore related articles here if you wish to learn more about Python and practice Python programming.
Contributed By: Prerna Singh
FAQs
What happens if I call capitalize() on an empty string?
If you call capitalize() on an empty string, it will return an empty string.
Does capitalize() modify the original string?
No, capitalize() does not modify the original string. It returns a new string with the first character capitalized and the other characters in lowercase.
How does capitalize() handle non-alphabetic characters?
capitalize() only modifies the case of alphabetic characters. Non-alphabetic characters remain unchanged.
Can I chain multiple string methods together, including capitalize()?
Yes, you can chain multiple string methods together, including capitalize(). However, it is crucial to remember that each method in the chain returns a new string, so the order in which you apply the methods can affect the final result.
Is capitalize() case-sensitive?
Yes, capitalize() is case-sensitive. It only capitalizes the first character of a string if it is alphabetic and lowercase. If the first character is already uppercase, it will remain unchanged.
Can I use capitalize() on a list of strings?
No, capitalize() is a string method and can only be used on individual strings. If you want to apply capitalize() to all strings in a list, you can use a loop or a list comprehension.
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio