Back to articles list Articles
7 minutes read

How to Uppercase the First Letter of a Word in Python

Start manipulating textual data by learning how to uppercase the first letter in Python.

In this article, we focus on a specific string operation in Python: how to change the letter case of the first letter in a word.

A word may consist of uppercase and lowercase letters. We need to consider this because "Python" and "python" are two different strings in Python. Here is an article that discusses the case-sensitiveness of Python in great detail.

Python is a general-purpose language. It has many applications in a variety of fields, such as web development, mobile game development, task automation, data engineering, and data science.

Python is the preferred language for data engineering and data science. There are lots of third-party Python libraries that expedite and simplify tasks in these areas. In addition to the rich selection of libraries, the base Python offers numerous functionalities to help with data operations.

The determining factor in data operations is the data type. How we manipulate numeric data is very different from working with textual data. Considering a substantial amount of data is textual, it is of crucial importance to manage and manipulate strings efficiently.

LearnPython.com has an entire interactive course on Working with Strings in Python. One of the great advantages of learning from interactive online courses is that they offer an active engagement experience. You have a chance to practice while learning.

It is important to note that strings in Python are not just words. Rather, they are a sequence of characters. For instance, "e?h-d" is also a string. For the purpose of this article, we will work strictly with words.

There are different methods for converting the first letter of a word to uppercase in Python. Let's start with the title() method.

title()

This is a very simple method and is used as follows:

>>> mystring = "python"
>>> mystring.title()
'Python'

The title() method may also be used on strings with multiple words. In that case, the first letter of each word is converted to uppercase (i.e., title case).

>>> mystring = "learn python"
>>> mystring.title()
'Learn Python'

Here is another example:

>>> mystring = "how to uppercase the first letter of a word in python"
>>> mystring.title()
'How To Uppercase The First Letter Of A Word In Python'

What the title() method does is to make a title out of a given string. Thus, each word starts with an uppercase letter. This is also known as pascal case where each word starts with a capital letter. So, you can also do pascal case in Python.

capitalize()

Another technique for this task is the capitalize() method. It is used just like the title() method.

>>> mystring = "python"
>>> mystring.capitalize()
'Python'

Although it seems like the capitalize() and title() methods do the same thing, there is a very important distinction between them. The capitalize() method only converts the first letter of the string, not every word. Thus, it takes a string as a single "word" regardless of its length and the number of real words it contains. Here is an example that demonstrates this distinction:

>>> mystring = "learn python"
>>> mystring.capitalize()
'Learn python'

We know the capitalize() method converts only the first letter of a string to uppercase in Python. However, we can combine it with the split() method to capitalize each word. The split() method, as its name suggests, splits a string at the positions of a given character. The following example demonstrates how to use the split() method to split a string with multiple words.

>>> mystring = "learn python"
>>> mystring.split(" ")
['learn', 'python']

We have used the space character as the split point. The output is a list that contains each item after splitting. We can access the items in a list by using an index. In the output list, the index of "learn" is 0, and the index of "python" is 1.

>>> mystring = "learn python"
>>> mystring.split(" ")[0]
'learn'
>>> mystring.split(" ")[1]
'python'

We have managed to access each word in a string with multiple words. The next step is to use the capitalize() method to convert the first letter to uppercase. Then, we combine the capitalized words into a single string.

>>> mystring = "learn python"
>>> mystring.split(" ")[0].capitalize() + mystring.split(" ")[1].capitalize()
'LearnPython'

As we see in the above example, it is very simple to combine strings by using the "+" sign. There is a small problem in the output though. The capitalized string is missing the space between the words. We can solve this issue by adding a space in between as follows:

>>> mystring = "learn python"
>>> mystring.split(" ")[0].capitalize() + " " + mystring.split(" ")[1].capitalize()
'Learn Python'

upper()

Python has the upper() method for uppercasing strings. When used on a string, it converts all the letters to uppercase.

>>> mystring = "python"
>>> mystring.upper()
'PYTHON'

>>> mystring = "python3"
>>> mystring.upper()
'PYTHON3'

To use the upper() method to convert only the first letter, we need additional operations. First, we need to select the first letter, then apply the upper() method to it. It is then combined with the rest of the characters.

A string in Python is a sequence of characters. So, we can access each character using an index. The index of the first character is 0.

>>> mystring = "python"
>>> mystring[0]
'p'

We can also use indices to extract a slice from a string. A slice is determined by specifying the starting and ending index. For instance, we can extract the first three letters from a string as below.

>>> mystring = "python"
>>> mystring[0:3]
'pyt'

The lower bound is inclusive and the upper bound is exclusive. So, [0:3] selects the characters at indices 0, 1, and 2. If we just want to take a slice that starts at the beginning of the string, specifying the lower bound is optional. Thus, [:3] is the same as [0:3].

>>> mystring = "python"
>>> mystring[:3]
'pyt'

Similarly, if we want to select every character until the end of the string, we do not have to indicate the upper bound. For instance, we can select everything starting from the second character as follows:

>>> mystring = "python"
>>> mystring[1:]
'ython'

We have seen how to select the first character and the remaining ones separately. To complete our task of capitalizing a word using the upper() method, we need to apply the upper() method to the first character and then combine it with the rest.

>>> mystring = "python"
>>> mystring[0].upper() + mystring[1:]
'Python'

istitle() and isupper()

In some cases, we need to check if a word starts with an uppercase letter. This is obvious when we see the word. However, when we need to process large amounts of data, a visual check is impossible. A method that can be implemented in code is needed.

The istitle() method does this control for us.

>>> mystring = "Python"
>>> mystring.istitle()
True

>>> mystring = "python"
>>> mystring.istitle()
False

It returns True if the first letter is uppercase and False otherwise. It also works on strings with multiple words.

>>> mystring = "Learn python"
>>> mystring.istitle()
False

>>> mystring = "Learn Python"
>>> mystring.istitle()
True

As we see in the examples above, the istitle() method checks each word and returns True only if all the words start with an uppercase letter. Even if only one word starts with a lowercase letter, the output is False.

This check may be done at the character level by using the isupper() method. We select the first letter of a string and then apply the isupper() method.

>>> mystring = "python"
>>> mystring[0].isupper()
False

>>> mystring = "Learn"
>>> mystring[0].isupper()
True

The output is in the same format as the output of the istitle() method. Since the isupper() method works at the character level, it is not practical to use it on strings with multiple words.

Learn to Change Letter Case in Python

These are the most common operations when working with textual data. We have covered different methods to uppercase the first letter in Python. This is a specific use case of string manipulation.

Python is a highly popular language used in many different domains. If you work or plan to work in the field of data science, it is your go-to language. LearnPython.com offers a smooth and efficient learning experience for Python.

If you are new to Python or have never done programming before, start with our Python Basics – Part 1 course. Then move on to the Learn Programming with Python track. It introduces you to the fundamentals of programming. You do not need to have any prior experience with IT. This track consists of 5 fully interactive courses, carefully organized and presented for beginners.

If you'd like to learn more about string manipulation with Python, I suggest taking the Working with Strings in Python course that teaches you how to:

  • Join, iterate, and slice strings.
  • Use popular string functions.
  • Format string values in the output.

LearnPython.com has many other interactive online courses that offer an active engagement experience. Don't wait to start a promising career in programming!