+3 votes
in Programming Languages by (14.7k points)

I want to capitalize all elements of a list. How can I do it?

e.g. ['hello','brother','how','are','you'] should result into ['Hello','Brother','How','Are','You']

1 Answer

+1 vote
by (15.2k points)

You can use either title() or capitalize() function to capitalize the elements of your list. See the following example.

>>> a=['hello','brother','how','are','you']
>>> [e.title() for e in a]
['Hello', 'Brother', 'How', 'Are', 'You']
>>> [e.capitalize() for e in a]
['Hello', 'Brother', 'How', 'Are', 'You']