![]() |
VOOZH | about |
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.
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:
HellO WorlD
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:
WelcomE TO GeeksforgeekS
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:
HellO WorlD