Python: Convert all the characters in uppercase and lowercase and eliminate duplicate letters from a sequence
6. Case Conversion & Dedup Map
Write a Python program to convert all the characters into uppercase and lowercase and eliminate duplicate letters from a given sequence. Use the map() function.
Sample Solution:
Python Code :
# Define a function named change_cases that converts a character to its upper and lower cases
def change_cases(s):
return str(s).upper(), str(s).lower()
# Create a set named 'chrars' containing characters
chrars = {'a', 'b', 'E', 'f', 'a', 'i', 'o', 'U', 'a'}
# Print the original set of characters
print("Original Characters:\n", chrars)
# Use the map function to apply the change_cases function to each character in 'chrars'
result = map(change_cases, chrars)
# Print a message indicating the operation to be performed
print("\nAfter converting above characters in upper and lower cases\nand eliminating duplicate letters:")
# Print the result of the map operation as a set, eliminating duplicate letters
print(set(result))
Sample Output:
Original Characters: {'f', 'b', 'U', 'i', 'o', 'E', 'a'} After converting above characters in upper and lower cases and eliminating duplicate letters: {('U', 'u'), ('O', 'o'), ('A', 'a'), ('B', 'b'), ('F', 'f'), ('I', 'i'), ('E', 'e')}
For more Practice: Solve these Related Problems:
- Write a Python program to convert each string in a list to both uppercase and lowercase, then remove duplicate characters using map.
- Write a Python program to map a lambda that returns a tuple of (uppercase, lowercase, sorted unique characters) for each string in a list.
- Write a Python program to convert each string into a list of unique characters (ignoring case) and sort them alphabetically using map.
- Write a Python program to transform each string into its reversed uppercase version with duplicates removed using the map() function.
Go to:
Previous: Write a Python program to square the elements of a list using map() function.
Next: Write a Python program to add two given lists and find the difference between lists. Use map() function.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.