How to get value from dictionary with leading or trailing spaces lowercase or uppercase key in Python

2 Answers

0 votes
def get_value(key):
    return key.strip().lower()

dic = {}

dic[get_value("  CPP")] = 113

print(dic[get_value("cpp  ")])
print(dic[get_value("Cpp  ")])
print(dic[get_value("CPP  ")])

print(dic[get_value("  cpp  ")])
print(dic[get_value("  Cpp")])
print(dic[get_value(" CPP  ")])


'''
run:

113
113
113
113
113
113

'''

 



answered Sep 5, 2018 by avibootz
0 votes
def get_value(key):
    return key.strip().lower()

dic = {}

dic[get_value("cpp")] = 120

print(dic[get_value("cpp  ")])
print(dic[get_value("Cpp  ")])
print(dic[get_value("CPP  ")])

print(dic[get_value("  cpp  ")])
print(dic[get_value("  Cpp")])
print(dic[get_value(" CPP  ")])


'''
run:

120
120
120
120
120
120

'''

 



answered Sep 5, 2018 by avibootz
...