Python Practice › List Programs › Program 94

Capitalise the 3rd character of each word

EasyList Programs · 94 of 100

Write a Python program to make the 3rd character of each word uppercase.

Advertisement
Input
["Hello", "student", "are", "learning", "Python", "Its", "Python", "Language"]
Output
['HeLlo', 'stUdent', 'arE', 'leArning', 'PyThon', 'ItS', 'PyThon', 'LaNguage']

Approach

  1. Index 2 is the 3rd character; rebuild the word around it.

Solution

Python
words = ["Hello", "student", "are", "learning", "Python", "Its", "Python", "Language"]print([w[:2] + w[2].upper() + w[3:] if len(w) > 2 else w for w in words])
Output
['HeLlo', 'stUdent', 'arE', 'leArning', 'PyThon', 'ItS', 'PyThon', 'LaNguage']
Note

The original sample changed the 4th character (“HelLo”). The 3rd character is index 2, which gives “HeLlo”.

Learn the concept

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

More list programs

← All 100 List Programs

Leave a Comment