Skip to content

How to capitalize elements in list Python | Example code

  • by

Use string upper() method to convert every element in a list of strings into uppercase (capitalize) in Python code.

Example capitalize elements in list Python

Simple python example code.

Using an upper() method with Loop

Use a Python for loop to iterate through each item in a given list of strings by index. Then use the string upper() method on each string and reassign it to the corresponding index in the list for that string.

list1 = ["a", "b", "c"]

for i in range(len(list1)):
    list1[i] = list1[i].upper()

print(list1)

Output:

How to capitalize elements in list Python

Use a list comprehension

[x.upper() for x in ["a","b","c"]]

Example code

list1 = ["a", "b", "c"]

list1 = [each_string.upper() for each_string in list1]

print(list1)

Using a map with lambda

list1 = map(lambda x: x.upper(), ["a", "b", "c"])

print(list(list1))

Do comment if you have any doubts and suggestions on this Python list code.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading