How to Capitalize First and Last Letters of Each Word in a String in Python [8 Ways]

Are you looking to capitalize first and last letters of each word in your Python strings? In this Python tutorial, we will cover nine different approaches to capitalize first and last letters of each word in a string in Python.

Capitalizing the first and last letters of each word is part of text processing in Python. We will discuss various Python techniques such as built-in string methods, list comprehensions, regular expressions, and more to accomplish this task effectively.

Let’s dive into these methods to learn how to implement:

  • Using the title() method in combination with slicing
  • Using the capitalize() method and string slicing
  • Using a for loop with slicing
  • Utilizing list comprehension with slicing
  • Applying Regular Expressions for customized capitalization
  • Implementing a while loop with slicing
  • Creating a custom function for specialized capitalization rules

Capitalize First and Last Letters of Each Word in a String in Python using title() method and slicing

The title() method is used to capitalize the first letter of each word in the string in Python.

Then, we can apply slicing in Python to capitalize each word’s last letter by connecting the second character to the last character of each word and concatenating it with the capitalized substring.

Here is an instance to capitalize first and last letters of each word in a string in Python using title() with slicing:

Code

text = "the quick brown fox jumps over the lazy dog"
capitalized_text = text.title()
capitalized_text = capitalized_text[:-1] + capitalized_text[-1].upper()
print(capitalized_text)

Output

ThE QuicK BrowN FoX JumpS OveR ThE LazY DoG

The code implementation is shown below as a screenshot:

program to capitalize first and last letter of string in python

How to use the capitalize() method and string slicing in Python

The capitalize() method is used for capitalizing the first letter of a string in Python. By combining it with string slicing, we can extend its functionality to capitalize the last letter of each word.

This is a similar approach to the previous example

Here is an example of capitalizing the first and last letters of each word in a string in Python.

Code

sentence = "the united States of America is a diverse country"
capitalized_sentence = ' '.join(word[:-1].capitalize() + word[-1].upper() for word in sentence.split())
print(capitalized_sentence)

Output

ThE UniteD StateS Of AmericA I S A DiversE CountrY

After implementing the code in the Python editor, the below the screenshot is taken:

capitalize first and last letter in python

Capitalize the First and Last letter of every Word using for loop in Python

A for loop provides flexibility in manipulating strings character by character. By combining it with slicing, we can efficiently capitalize the first and last letters of each word.

Code

sentence = "los angeles is known for its entertainment industry"
capitalized_sentence = ""
for word in sentence.split():
    capitalized_word = word[0].upper() + word[1:-1] + word[-1].upper()
    capitalized_sentence += capitalized_word + " "
print(capitalized_sentence)

We split the sentence into words and iterate over each word in Python. We capitalize the first letter using slicing and capitalize the last letter separately for each word. Then, we concatenate both parts to form the capitalized Python string.

Output

LoS AngeleS Is KnowN FoR ItS EntertainmenT IndustrY 

The following screenshot illustrates the result after executing the code in the Python editor.

How to capitalize first and last letter of each word in python

Python First and Last letter of Each Word Capitalize using List Comprehension

List comprehension offers a concise and efficient way to manipulate lists in Python. By combining it with slicing, we can capitalize the first and last letters of each word in a string.

Here is an example:

Code

sentence = "san francisco is famous for its iconic landmarks"
capitalized_sentence = ' '.join(word[0].upper() + word[1:-1] + word[-1].upper() for word in sentence.split())
print(capitalized_sentence)

We split the sentence into words using split() in Python, then use list comprehension to iterate over each word.

We capitalize the first letter using slicing and capitalize the last letter separately for each word. Then, we joined the capitalized words into a single string.

Output

SaN FranciscO Is FamouS FoR ItS Iconic LandmarkS

The resulting output is displayed in the screenshot below after running the code in Python editor.

string capitalize first and last letter in Python

Uppercase First and Last letter of Every Word in Python using Regular Expression

Regular expressions provide a powerful way to match and manipulate strings based on specific patterns. By defining a pattern to match the first and last letters of each word, we can capitalize them accordingly.

Here is the instance to capitalize the first and last word of each Python string:

Code

import re

text = "the grand canyon is a must-visit destination"
capitalized_text = re.sub(r'\b(\w)(\w*)(\w)\b', lambda m: m.group(1).upper() + m.group(2) + m.group(3).upper(), text)
print(capitalized_text)

We use the re.sub() function to apply a custom lambda function to each match found by the regular expression pattern. The lambda function capitalizes both the first and last letters of each word.

Output

ThE GranD CanyoN Is A Must-VisiT DestinatioN
capitalize first and last letter of each word in python

Using While Loop with Slicing to Capitalize First and Last Index Values in Python

A while loop offers flexibility in controlling iterations based on specific conditions. By combining it with slicing, we can efficiently capitalize first and last letters of each word in a string in Python.

Code:

sentence = "the golden gate bridge is an iconic landmark"
words = sentence.split()
index = 0
while index < len(words):
    words[index] = words[index][0].upper() + words[index][1:-1] + words[index][-1].upper()
    index += 1
capitalized_sentence = ' '.join(words)
print(capitalized_sentence)

We split the sentence into words and initialized an index variable. We iterate over each word using a while loop, capitalize the first and last letters using slicing, and update the word in the list.

Finally, we join the capitalized words into a single string.

Output:

ThE GranD CanyoN Is A Must-VisiT DestinatioN

After the code was implemented in the Python editor, the subsequent screenshot was taken to illustrate the output

Capitalize First and Last Letters of Each Word in a String in Python

Create a Custom Function to Capitalize the first and last Letter of Each word in Python

We can create a custom function that allows us to define specialized capitalization to get the required result.

By encapsulating the capitalization logic within a function in Python, we can reuse it across different strings and scenarios.

Here is the instance:

Code

def capitalize_first_and_last_letters(sentence):
    capitalized_words = [word[0].upper() + word[1:-1] + word[-1].upper() for word in sentence.split()]
    return ' '.join(capitalized_words)

sentence = "the grand canyon is a natural wonder"
capitalized_sentence = capitalize_first_and_last_letters(sentence)
print(capitalized_sentence)

We define a custom function capitalize_first_and_last_letters() that takes a string as input.

Within the Python function, we split the string into words, capitalized first and last letters of each word in a string in Python, and joined the capitalized words into a single string.

Output

ThE GranD CanyoN Is A NaturaL WondeR

The screenshot below presents the output obtained after the code execution in the Python editor.

How to capitalize first and last letter of each word string in python

Conclusion

Understanding multiple different methods and ways to capitalize first and last letters of each word in a string in Python, with different practical examples, can help to solve their problem easily.

As each method offers distinct advantages and flexibility, from built-in string methods like title() and capitalize() to advanced methods such as regular expressions, list comprehensions, and custom functions.

You may also like to read these articles: