In this Python tutorial, I will show you what is the Capitalize function in Python string. We will see Syntax, Parameters, and return values for the capitalize method in String Python. We will also see some examples showing the uses of the string capitalize() method.
Python string is a sequence of characters. Strings in Python are used for representing and manipulating textual data. We can create a string in Python by enclosing characters in quotes. Python treats single quotes the same as double quotes. Here is how to create a string in Python:
City = 'California' # or Country = "United states of America"
Python has several built-in methods that can perform operations on strings. One of them is capitalize() function.
The Capitalize() Method in Python string
The capitalize() function is a built-in Python method that can be applied to string objects. This function converts the string’s first character to uppercase, if it is a lowercase letter, and turns all other characters in the Python string into lowercase.
The basic syntax of the capitalize() function in Python string:
In the above syntax, ‘string‘ represents the Python string on which we want to perform the operation.
The string.capitalize() method does not take any parameters.
The string.capitalize() function in Python returns a copy of the original Python string and converts the string’s first character to a capital (uppercase) letter while making all other characters in the string lowercase.
Note: A few important aspects to consider when using the Python capitalize() function are:
- Immutability of strings: Python strings are immutable, meaning that they cannot be changed once they are created. When we use the capitalize() function, it doesn’t modify the original string. Instead, it creates a new Python string with the changes.
- Non-alphabetic characters: If the string starts with a non-alphabetic character, the capitalize() function leaves it as it is, and the first letter after the non-alphabetic character will be capitalized.
- Handling of uppercase letters: If the first character of the string is already in uppercase, the capitalize() function leaves it as it is. However, all other characters in the string will still be converted to lowercase.
Python string capitalize() function examples
The Capitalize() function in Python has a variety of applications. Here are some practical examples that demonstrate its usage:
Example-1: String Formatting
The Basic use of Python’s String Capitalize Function is to format a given string. For example, let’s take a city name as a string which is not properly capitalized:
city = 'neW YoRk' city_after_capitalized = city.capitalize() print(city_after_capitalized)
The output will be: By using the capitalize() function, Python returns a string- with the first character of the string in uppercase and the rest of the characters in lowercase.
This way we can use the capitalize method to get a string in Proper format in Python.
Example-2: User data input
This can be the best way to save some data in a format that is going to be entered by the User. As we don’t know what way they are going to enter the data.
For instance, Let’s take an entry data of employees city by them in Python and store that data in a List in required format(capitalized string).
cities =  list_length = 5 for x in range(list_length): Employee_city = input('Enter your city name: ') cities.append(Employee_city.capitalize()) print(cities)
Here, we have created an empty list(cities). Then with the help of for loop, we iterated over the strings, which the user has given(taking input under the for loop) and then added the capitalized strings to the empty list.
The output is:
Enter your city name: NEW YORK Enter your city name: California Enter your city name: alaska Enter your city name: 12Georgia Enter your city name: hAWaI ['New york', 'California', 'Alaska', '12georgia', 'Hawai']
Note: I have taken all the string in different format to show you what a capitalize() methods in Python string.
This way we can use the string capitalize method to store the user input data in a fixed format in Python.
Example-3: Formatting a pervious stored data
Let’s take an example, where we have a list of names of some players of soccer in the USA. Players’ names typically follow the pattern of having the first letter of each name capitalized.
players = ['JeSUs FErrEira', 'Matt Turner', 'AARON LONG', 'john tolkin'] corrected_players_name = [name.capitalize() for name in players] print(corrected_players_name)
The output is:
['Jesus ferreira', 'Matt turner', 'Aaron long', 'John tolkin']
This way we can use Python capitalize() method to capitalize all the strings saved in the list.
Note: The capitalize() function only capitalizes the string’s first character. So, if we have multi-word strings, it might not work as expected. For instance, in the case of ‘john tolkin’, only ‘J’ in ‘john’ is capitalized, but ‘t’ in ‘tolkin’ is not. To capitalize each word in a string, Python offers the title() function.
The capitalize() function is a handy tool in Python for manipulating string data, especially when it comes to text preprocessing and formatting. It allows for easy conversion of the first letter to uppercase, helping to standardize strings in our data for consistent analysis or display. As seen from our examples, it’s essential to understand the context and requirements of our data to choose the appropriate string method in Python.
You may also like to read:
- Upper function in Python string | Python string uppercase() method with examples
- Lower function in Python string
- Find function in Python string
- Casefold function in Python string
- Center function in Python string
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.