How to Map Strings to Uppercase in Python
Loops through a list of strings and builds a new list with each string converted to uppercase.
Python code
7 linesstrings = ["hello", "world", "python", "skillset"]
uppercased = []
for s in strings:
uppercased.append(s.upper())
print(uppercased)
Output
['HELLO', 'WORLD', 'PYTHON', 'SKILLSET']
How it works
The for loop iterates over each string in the original list. Inside the loop, s.upper() creates a new string with all characters converted to uppercase, which is then appended to uppercased. This is a classic mapping pattern: one input sequence, one output sequence, preserving order. The original list remains unchanged because strings are immutable and a new list is built.
Common mistakes
- Forgetting to initialize `uppercased` before the loop, causing a NameError
- Modifying the original list while iterating over it, leading to skipped items
- Using `print()` inside the loop, which prints each item instead of a final list
Variations
- Use a list comprehension: `uppercased = [s.upper() for s in strings]`
- Use the `map()` function with lambda: `list(map(lambda s: s.upper(), strings))`
Real-world use cases
- Normalizing user input like usernames or tags to uppercase before storing or comparing in a database.
- Transform a list of city names fetched from an API into a consistent uppercase format for reporting.
- Converting command names in a CLI tool to uppercase for case-insensitive dispatch.
Sponsored
More from Algorithms & data structures
- Binary Search for Ship Capacity in Python medium
- Binary Search on Answer in Python: Koko Eating Bananas medium
- Bucket Numbers into Histogram Bin Counts in Python easy
- Container With Most Water: Two-Pointer Solution in Python medium
- Count Smaller Elements to the Right in Python easy
- Depth First Search Traversal Order in Python easy
Keep learning
Related tutorials and quizzes for this topic.