+3 votes
in Programming Languages by (75.1k 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

+2 votes
by (71.8k points)
selected by
Best answer

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']