How To Capitalize All Words in a String in Python

Convert “hello world” to “Hello World”

Jonathan Hsu
Jan 7 · 3 min read
Photo by Alexander Andrews on Unsplash

For this task, we’ll be using a handful of Python techniques in order to split, modify, and rebuild our string so that the first letter in each word is capitalized.

Let’s start with a brief overview of each puzzle piece, so we can review the solution more clearly.

  • The .split() String Method: This method will convert a string into a list, taking a single optional parameter as the delimiter to split the string by. If no delimiter is specified, then any white space will be used to split the string.
  • List Comprehension: A truly powerful tool in your Python tool belt, list comprehensions are a way of creating a list from an iterable and an optional conditional expression. Each item from the iterable may be modified within the comprehension, allowing for extremely concise and performant code.
  • The .capitalize() String Method: This method will return the string with its first letter capitalized.
  • The .join() String Method: This method will take a given list and convert it to a string, using the supplied value as a delimiter. If you come from a JavaScript background, you may be used to .join() being an array method — however, in python, it is a string method. The string delimiter is supplied as the base object and the list is passed as an argument to .join().

The Solution

Now that we’re more familiar with each piece of the solution, let’s get into it.

We first set our string value and then split it into a list:

message = "hello world"parts = message.split(" ")

We separate each word by passing an empty space to .split(). If we were splitting a sequence of comma-separated values the method call would be .split(",").

Next, we will use a list comprehension alongside the .capitalize() method to modify the first letter of each string:

capitalized_parts = [p.capitalize() for p in parts]

What’s happening here is we loop through parts, assigning each item to the variable p. Then the resolution of p.capitalize() is passed to our newly created list.

Finally, we convert from our list back to a string:

capitalized_message = " ".join(capitalized_parts)

This takes some getting used to, but you first supply the delimiter as a string — in our case a blank space — then call the .join() method with the list as an argument.

So far we’ve moved one step at a time for clarity. However, the entire solution can be condensed into a single statement — awesome, right?

message = "hello world"capitalized_message = " ".join([
word.capitalize()
for word in message.split(" ")
])
print(capitalized_message) # Hello World

If you are inclined to do so, you could easily take this statement and create a function that accepts two arguments — the string and the delimiter — to reuse when necessary.


Do you have a preferred way of capitalizing all words in a string? What other similar transformations do you perform on text using list comprehensions? Share your comments and feedback below!

Better Programming

Advice for programmers.

Jonathan Hsu

Written by

I love to share my experiences learning to code, working with customers, and managing my blog. Sign up for my newsletter at https://j-hsu.com

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade