# Define a dictionary
car = {
"Modal": "X1s",
"Showroom Address": "India",
"Brand": "TATA",
"Car Type": "Petrol"
}
# Convert dictionary keys to lowercase
car = {key.lower(): val for key, val in car.items()}
# Print the result
print(car)
In the above code snippet, we are using dict comprehension to convert all the keys of a dictionary(car) to lowercase letters. We are also using the lower() function of Python String to convert the key text to lowercase.
If you do not want to use dict comprehension then you can use the below code to convert dictionary keys to lowercase format.
car = {
"Modal": "X1s",
"Showroom Address": "India",
"Brand": "TATA",
"Car Type": "Petrol"
}
result = {}
for key, value in car.items():
result[key.lower()] = value
# Print the result
print(result)
Output
{'modal': 'X1s', 'showroom address': 'India', 'brand': 'TATA', 'car type': 'Petrol'}
0 Comments