Skip to content

Convert a String to Title Case in Python with str.title()

str title() and str istitle Convert a String to Title Case in Python Cover Image

Being able to work with strings is an essential skill for a Python developer of any skill level. One of the most common operations you’ll run into is needing to capitalize different characters. For example, you may want to convert a string to title case, meaning that the first letter of each word in a string is capitalized.

In this tutorial, you’ll learn how to use the str.title() and str.istitle() methods to convert a string to title case in Python. This can be incredibly helpful if you’re renaming files in Python and want to clean up their file names.

By the end of this tutorial, you’ll have learned the following:

  • How to convert a string to title case in Python using the str.title() method
  • How to work with problematic strings, such as those with apostrophes using the str.capcase()
  • How to check if a string is already title case in Python using the str.istitle() method

Want to learn how to capitalize a string in Python in different ways? Check out my complete guide to capitalizing strings in Python.

How to Convert a String to Title Case in Python with str.title()

Converting a string to title case means capitalizing the first letter of each word, while the remaining letters are lowercase. For example, the string 'welcome to datagy' would be converted to 'Welcome To Datagy'. This is such a common operation that Python comes with a built-in method for handling this.

How can you convert a string to title case in Python?

To convert a string to title case in Python, you can use the str.title() method, which will capitalize each word in a string. Because strings are immutable, the old string is destroyed and a new string is returned.

Let’s take a look at an example of how you can use Python to convert a string to title case:

# Convert a String to Title Case in Python
text = 'welcome to datagy'
title = text.title()

print(f'{text} --> {title}')

# Returns:
# welcome to datagy --> Welcome To Datagy

Let’s break down what we did in the code block above:

  1. We loaded a string using the variable text
  2. We then created a new string, title, which is the result of applying the .title() method to the original string
  3. Finally, we printed our two strings in order to compare them

Keep in mind that Python strings are immutable, meaning that they cannot be modified. In the example above, we assigned the transformed string to a new variable. That said, we could simply reassign the string to itself. This would destroy the former string and create a new one with the text in title case.

How to Convert a String to Title Case with Apostrophes in Python with string.capwords()

The Python str.title() method will identify any separated word as a new word. Because of this, the method can lead to some unexpected results. For example, let’s take a look at the string: "Let's visit datagy.io!":

# Some Issues Arise with Apostrophes and Periods
text = "Let's go to datagy.io!"
title = text.title()

print(f'{text} --> {title}')

# Returns:
# Let's go to datagy.io! --> Let'S Go To Datagy.Io!

When we see what Python returns, it’s not exactly what we had intended! The 's' in Let’s is capitalized, for example. Similarly, the I in .io is capitalized as well.

Because Python is meant to be language independent, it uses a basic method to identify new words. In order to fix this, we can use the built-in string.capwords() function. In order to do this, we first need to import the function:

# Convert a String to Title Case in Python
from string import capwords
text = "Let's go to datagy.io!"
title = capwords(text)

print(f'{text} --> {title}')

# Returns:
# Let's go to datagy.io! --> Let's Go To Datagy.io!

In the code block above, we imported the capwords() function from the string library. We then repeated our earlier code, though we passed the original string into the function.

Under the hood, the function splits the string based on a provided delimiter. It then capitalizes the first letter and joins the strings back together.

Using Regular Expressions to Title Case a String in Python

Similar to the example above, we can actually use regular expressions to convert a problematic string to title case. Personally, I prefer the method above, as regular expressions can be a bit complicated to read. However, I wanted to include the example for completeness.

Per the Python documentation, we can write the following function to use regular expressions to title case a string:

# Using Regular Expressions to Title Case a String
import re

def titlecase(s):
    return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", lambda mo: mo.group(0).capitalize(), s)

text = "Let's go to datagy.io!"
title = titlecase(text)

print(f'{text} --> {title}')

# Returns:
# Let's go to datagy.io! --> Let's Go To Datagy.Io!

In this case, we specified that Python should ignore characters following an apostrophe character. We can see from the code block above that this worked as intended. However, it did also capitalize the character following the period.

Like mentioned, I find this method unnecessarily complex. The capwords() function from the string library is significantly simpler to implement and much easier to understand.

How to Check if a String is Title Case in Python

Python also provides a helpful method to check if a string is already title case. The str.istitle() method doesn’t accept any arguments, but returns a boolean value. The return value indicates whether or not a string is title cased or not.

Let’s take a look at an example. We’ll provide two strings: one that is title cased and one that isn’t. We can then use the .istitle() method to check if they are title cased:

# Checking if a String is Title Cased in Python
titled = 'Welcome To Datagy!'
not_titled = 'welcome To Datagy'

print(f'titled is title case: {titled.istitle()}')
print(f'not_titled is title case: {not_titled.istitle()}')

# Returns:
# titled is title case: True
# not_titled is title case: False

Let’s break down what we’re doing in the code block above:

  1. We created two strings, one that was title cased and one that wasn’t
  2. We then used f-strings to print out whether a string is title case or not by applying the .istitle() method to both strings

The method is quite simple but allows you to easily check the status of your strings or whether or not a transformation worked.

Conclusion

In this tutorial you learned how to title case a string using Python. Being able to work with strings is a useful skill for any Python developer. For example, when you’re renaming files, you may want to title case the file names.

You first learned how to use the string .title() method to convert a string to its title-cased version. Then, you learned how to customize the behavior when working with trickier strings such as those with apostrophes or periods using the string.capwords() function and regular expressions. Finally, you learned how to use the istitle() method to check if a string is already title-cased.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *