Python Practice › String Programs › Program 71

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.

Advertisement
Input
"this is my first program"
Output
ThiS IS MY FirsT PrograM

Approach

  1. 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
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs