Capitalize the first letter of each word in List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 7, 2022

banner

Photo from Unsplash

Capitalize the first letter of each word in List in Python #

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

  1. Use a list comprehension to iterate over the list.
  2. Use the str.capitalize() method to capitalize the first letter of each word.
  3. All items in the new list will have a capital first letter.
main.py
from string import capwords # ✅ Capitalize the first letter of each word in list my_list = ['bobby', 'hadz', 'com'] result = [item.capitalize() for item in my_list] print(result) # 👉️ ['Bobby', 'Hadz', 'Com'] # ------------------------------------------ # ✅ Capitalize the first letter of each word in string my_str = 'bobby hadz com' result = capwords(my_str) print(result) # 👉️ 'Bobby Hadz Com'

We used a list comprehension to iterate over the list of strings.

List comprehensions 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.capitalize() method to capitalize the first character of each string.

The str.capitalize function returns a copy of the string with the first character capitalized and the rest lowercased.

main.py
print('bobby'.capitalize()) # 👉️ 'Bobby' print('HADZ'.capitalize()) # 👉️ 'Hadz'

The str.capitalize() method only converts the first letter of each item in the list to uppercase.

If you need to capitalize all words in each list item, use the string.capwords() method.

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']

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.
The example that uses the str.capitalize() method only converts the first letter in each list item to uppercase.

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

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

Note that there is also a str.title() method.

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.

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

However, 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"
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.