Capitalise the first and last letter of each word
EasyString Programs · 71 of 106
Write a Python program to capitalise the first and last letters of each word.
Input"this is my first program"
OutputThiS IS MY FirsT PrograM
Approach
- For each word: upper first + middle + upper last.
Solution
Python
def cap_ends(w): if len(w) == 1: return w.upper() return w[0].upper() + w[1:-1] + w[-1].upper() print(" ".join(cap_ends(w) for w in "this is my first program".split()))
Output
ThiS IS MY FirsT PrograM
Learn the concept
New to this topic? Read the lesson, then come back.
More string programs
← All 106 String Programs