Python Practice › List Programs › Program 94
Capitalise the 3rd character of each word
Write a Python program to make the 3rd character of each word uppercase.
Input
["Hello", "student", "are", "learning", "Python", "Its", "Python", "Language"]
Output
['HeLlo', 'stUdent', 'arE', 'leArning', 'PyThon', 'ItS', 'PyThon', 'LaNguage']
Approach
- Index 2 is the 3rd character; rebuild the word around it.
Solution
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.