Python program to capitalize the first and last character of each word in a string Last Updated : 31 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 Comprehensionlist 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: Python s = "hello world" # Split `s` into words w = s.split() # process each word in the list `w` res = ' '.join([ # If the word has more than 1 character i[0].upper() + i[1:-1] + i[-1].upper() if len(i) > 1 else i.upper() # If word has only 1 char, capitalize the whole word for i in w # Iterate through each word in the list `w` ]) print(res) OutputHellO 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: Python s = "welcome to geeksforgeeks" res = ' '.join( map( lambda word: word[0].upper() + word[1:-1] + word[-1].upper() # Capitalize first and last character if len(word) > 1 else word.upper(), # If word length is 1, capitalize the whole word s.split() # Split `s`into words ) ) print(res) OutputWelcomE 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: Python import re s = "hello world" res= re.sub(r'\b(\w)(\w*)(\w)\b', lambda match: match.group(1).upper() # Capitalize first character + match.group(2) # Keep the middle part as is + match.group(3).upper(), # Capitalize last character s) # Apply the substitution to the string print(res) OutputHellO WorlD Comment More infoAdvertise with us Next Article Python program to capitalize the first and last character of each word in a string H hemantupadhyay2 Follow Improve Article Tags : Technical Scripter Python Python Programs Technical Scripter 2019 python-string Python string-programs +2 More Practice Tags : python Similar Reads Python program to capitalize the first letter of every word in the file The following article contains programs to read a file and capitalize the first letter of every word in the file and print it as output. To capitalize the first letter we will use different methods using Python. The Python String Method is used to convert the first character in each word to Uppercas 3 min read How to Capitalize First Character of String in Python Suppose we are given a string and we need to capitalize the first character of it, for example:Input: "geeks"Output: "Geeks"In this article, we are going to learn several ways of doing it with examples, let's look at them:Using str.capitalize()Using str.capitalize() is a simple way to make the first 2 min read Python - Create a string made of the first and last two characters from a given string To solve the problem, we need to create a new string that combines the first two and the last two characters of a given string. If the input string is too short (less than 2 characters), we should return an empty string. This task is simple and can be achieved using a variety of methods.Using slicin 2 min read Python Program for Convert characters of a string to opposite case Given a string, convert the characters of the string into the opposite case,i.e. if a character is the lower case then convert it into upper case and vice-versa. Examples: Input : geeksForgEeksOutput : GEEKSfORGeEKSInput: hello every oneOutput: HELLO EVERY ONEExample 1: Python Program for Convert ch 6 min read Python program to find start and end indices of all Words in a String Given a String, return all the start indices and end indices of each word. Examples: Input : test_str = ' Geekforgeeks is Best' Output : [(1, 12), (16, 17), (19, 22)] Explanation : "Best" Starts at 19th index, and ends at 22nd index. Input : test_str = ' Geekforgeeks is Best' Output : [(1, 12), (17, 4 min read Like