Being able to work with strings in Python is an essential skill for a Pythonista of any level. In this tutorial, you’ll learn how to use Python to capitalize strings, including single words, title casing, and capitalizing lists of strings. In many cases, working with text in Python requires extensive data cleaning – knowing the ins and outs of different use cases can make you much more efficient.
By the end of this tutorial, you’ll have learned the following:
- How to use the
string.capitalize()
method to capitalize the first letter of a string - How to use the
string.title()
method to capitalize the first letter of every word in a string - How to use the
capwords()
function capitalize strings - How to capitalize lists of strings in Python
Want to learn about lower-casing a string instead? Check out my post on lowercasing strings in Python.
Table of Contents
Quick Answer: How to Capitalize Strings in Python
The table below breaks down the different methods and functions available in Python to capitalize strings:
Method | Example | Use Case |
---|---|---|
.capitalize() | 'datagy.io'.capitalize() | When only the first letter needs to be capitalized |
.title() | 'datagy.io'.title() | When every word needs to be capitalized |
.upper() | 'datagy.io'[0].upper() + 'datagy.io'[1:] | For interviews |
capwords() | capwords('datagy.io') | When you have unique separators |
Now that you have a quick overview of how to capitalize strings in Python, let’s dive into these methods in more detail.
How to Use Python capitalize() to Capitalize a String
The Python capitalize method is used to capitalize the first letter of a string. This allows you to easily capitalize sentences, if you’re given single sentences at a time. Because Python strings are immutable, you’ll need to reassign the string to itself.
Let’s take a look at what this looks like:
# Capitalize a String in Python
text = 'welcome to datagy!'
text = text.capitalize()
print(text)
# Returns: Welcome to datagy!
We can see that in the code block above, we applied the .capitalize()
method to the string. However, we also needed to re-assign the string. In this case, we re-assigned the string to itself.
Let’s now take a look at the .title()
method, which allows you to capitalize every word.
How to Use Python title() to Capitalize Every Word in a String
Python also provides a helpful method title capitalize every word in a string, also known as title-casing a string. While I cover the title method in more detail in another post, the title method allows you to capitalize every word in a string.
In order to capitalize every word in a string in Python, you can apply the .title()
method. Let’s take a look at what this looks like:
# Title Case a String in Python
text = 'welcome to datagy!'
text = text.title()
print(text)
# Returns: Welcome To Datagy!
Similar to the previous example, we needed to assign the string to another object. In this case, we re-assigned it to itself. We can see that each of the words in the string is capitalized.
How to Use the capwords() Function in Python
Part of Python’s standard library is the string library. The string library allows you to do things like access all punctuation so that you can remove punctuation from a string. The library also comes with the capwords()
function, which allows you to capitalize words.
What makes it different from the .title()
method you learned about earlier is that it allows you to set custom separators for what qualifies as a word.
Let’s take a look at a simple example of using the capwords()
function:
# Capitalize a String in Python
import string
text = 'welcome to datagy!'
text = string.capwords(text)
print(text)
# Returns: Welcome To Datagy!
In this example, we passed the string into the capwords()
function. This emulates the behavior of the .title()
method. In order to maximize the utility of the function, we can pass in a custom separator to identify where to capitalize strings. Let’s see what this looks like:
# Capitalize a String in Python
import string
text = "welcome to datagy-let's learn"
text = string.capwords(text, sep='-')
print(text)
# Returns: Welcome to datagy-Let's learn
We can see how this function provides a lot of clear usefulness in capitalizing strings.
How to Use upper() to Capitalize Strings in Python
The Python upper method is used to convert all letters to upper-case. This, in itself, isn’t useful when we want to only capitalize a single character. However, we can combine this with string slicing to capitalize strings in Python.
Personally, I find this example mostly useful for academic purposes, allowing you to show off different ways of capitalizing in interviews.
# Capitalize a String with upper()
text = 'welcome to datagy!'
text = text[0].upper() + text[1:]
print(text)
# Returns: Welcome to datagy!
We can see that this method is a bit more work (and, certainly, much less clear) than the previous methods you learned about. Because of this, I recommend using this method only when you need to.
How to Capitalize the First Letter of Every String in a List
In this final section, we’ll take a look at how to capitalize the first letter of every string in a list. This can be an incredible method to capitalize sentences or to manipulate multiple strings of data.
In order to capitalize the first letter of every string in a list, you can use a for loop and apply the capitalize method to each string.
Let’s take a look at what this looks like in Python:
# Capitalize a List of Strings
texts = ['welcome to datagy', 'learning is fun!', 'python rules']
capitalized = []
for text in texts:
capitalized.append(text.capitalize())
print(capitalized)
# Returns:
# ['Welcome to datagy', 'Learning is fun!', 'Python rules']
In the example above, we used a Python for loop to iterate over each string in the list and capitalize it. We then appended that item to a new list.
We can simplify this process by using a Python list comprehension, which saves us the trouble of first instantiating an empty list. Let’s see what this looks like:
# Capitalize a List of Strings Using a List Comprehension
texts = ['welcome to datagy', 'learning is fun!', 'python rules']
capitalized = [text.capitalize() for text in texts]
print(capitalized)
# Returns:
# ['Welcome to datagy', 'Learning is fun!', 'Python rules']
In the code block above, we simplified the code by using a list comprehension, which makes the code much more concise and easier to read.
Frequently Asked Questions
To capitalize the first letter of a string in Python, you can use the capitalize() method. This method is used to capitalize the first letter of a string and returns a new string with the first letter capitalized. To use the capitalize() method, you need to reassign the string to itself after applying the method.
The Python capitalize() method and the title() method are both used to capitalize strings in Python, but they have different use cases. The capitalize() method is used to capitalize the first letter of a string, while the title() method is used to capitalize the first letter of every word in a string.
To capitalize the first letter of every string in a list in Python, you can use a for loop and apply the capitalize() method to each string. Alternatively, you can use a list comprehension to achieve the same result in a more concise manner.
Conclusion
Capitalizing strings is a common task in Python programming, whether you’re working with single strings or lists of strings. Fortunately, Python provides several built-in methods and functions that make capitalizing strings easy and efficient. In this post, we explored four different methods for capitalizing strings in Python: the capitalize() method, the title() method, the capwords() function, and the upper() method with string slicing. We also covered how to capitalize the first letter of every string in a list using a for loop and a list comprehension.
To learn more about the string capitalize method, check out the official documentation.