VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-capitalize-the-first-and-last-character-of-each-word-in-a-string/

⇱ Python program to capitalize the first and last character of each word in a string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python program to capitalize the first and last character of each word in a string

Last Updated : 12 Jul, 2025

In this article, we will explore how to capitalize the first and last character of each word in a string in Python. It involves processing each word to transform its first and last letters to uppercase, while making the other characters lowercase.

Using List Comprehension

list comprehension and string slicing capitalize the first and last letters of each word in a sentence. It handles both short and long words, then joins them back into a single string.

Example:


Output
HellO WorlD

Using map()

map() function with lambda to capitalize the first and last letters of each word. It processes each word in the string and then joins them into a single result.

Example:


Output
WelcomE TO GeeksforgeekS

Using re.sub()

re.sub() method uses regular expressions to match the first and last characters of each word, applying capitalization to them. It efficiently processes the string in O(n) time complexity, where n is the length of the string.

Example:


Output
HellO WorlD
Comment