Capitalize first letter of each word in a String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 7, 2022

banner

Photo from Unsplash

Capitalize first letter of each word in a String in Python #

Use the string.capwords() method to capitalize the first letter of each word in a string, e.g. result = string.capwords(my_str). The capwords() method splits the string into words, capitalizes each word and joins the capitalized words into a string.

main.py
from string import capwords my_str = 'bobby hadz com' result = capwords(my_str) print(result) # 👉️ "Bobby Hadz Com"

We used the string.capwords() method to capitalize the first letter of each word in a string.

The string.capwords method:

  1. Uses the str.split() method to split the string into words.
  2. Uses the str.capitalize() method to capitalize each word.
  3. Uses the str.join() method to join the capitalized words into a string.
main.py
from string import capwords my_str = 'one two three' result = capwords(my_str) print(result) # 👉️ "One Two Three"

You can also use the capwords() method if you need to capitalize the first letter of each word in a list.

main.py
from string import capwords my_list = ['bobby hadz', 'dot com'] result = [capwords(item) for item in my_list] print(result) # 👉️ ['Bobby Hadz', 'Dot Com']

We used a list comprehension to iterate over the list and passed each item to the capwords() method.

If you don't want to convert the rest of each word to lowercase, define a custom function.

Capitalize first letter of each word in String using custom function #

To capitalize the first letter of each word in a string:

  1. Use the str.split() method to split the string into a list of words.
  2. Use the str.upper() method to capitalize the first letter in each word.
  3. Use the str.join() method to join the words into a string.
main.py
def capitalize_words(string): return ' '.join(word[0].upper() + word[1:] for word in string.split()) # 👇️ Bobby Hadz Com print(capitalize_words('bobby hadz com')) # 👇️ One Two Three print(capitalize_words('one two three'))

The first step is to use the str.split() method to split the string into a list of words.

main.py
print('bobby hadz com'.split()) # 👉️ ['bobby', 'hadz', 'com']

The str.split() method splits the string into a list of substrings using a delimiter.

When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.

We used a generator expression to iterate over the list of words.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.upper() method to convert the first character in the string to uppercase and append the rest of the characters.

If you need to lowercase the rest of the characters, use the str.lower() method.

main.py
def capitalize_words(string): return ' '.join(word[0].upper() + word[1:].lower() for word in string.split()) # 👇️ Bobby Hadz Com print(capitalize_words('BOBBY HADZ COM')) # 👇️ One Two Three print(capitalize_words('ONE TWO THREE'))

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

Alternatively, you can use the str.title() method.

Capitalize first letter of each word in String using str.title() #

Use the str.title() method to capitalize the first letter of each word in a string, e.g. result = my_str.title(). The str.title() method returns a titlecased version of the string where each word starts with an uppercase character and the remaining characters are lowercase.

main.py
my_str = 'bobby hadz com' result = my_str.title() print(result) # 👉️ Bobby Hadz Com

The str.title method returns a titlecased version of the string where the words start with an uppercase character and the remaining characters are lowercase.

However, note that the algorithm also converts characters after apostrophes to uppercase.

main.py
my_str = "it's him" result = my_str.title() print(result) # 👉️ "It'S Him"

The string.capwords() method doesn't have this problem, as it only splits the string on spaces.

main.py
from string import capwords my_str = "it's him" result = capwords(my_str) print(result) # 👉️ "It's Him"

Which approach you pick is a matter of personal preference. I'd go with the string.capwords method to avoid capitalizing characters after apostrophes.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.