What is the best way for me to create a dictionary from a string that key is each character and its upper case and value is its opposite case? I can use two line dictionary comprehensive but any better way? ex:
string: abc => {'a': 'A', 'b': 'B', 'c': 'C', 'C': 'c', 'B': 'b', 'A': 'a'}
string = 'abc'
d = { i:i.upper() for i in string}
d.update({ i.upper():i for i in string})
Use swapcase:
s = 'abc'
result = dict(zip(s + s.swapcase(), s.swapcase() + s))
print(result)
Output
{'C': 'c', 'b': 'B', 'B': 'b', 'a': 'A', 'A': 'a', 'c': 'C'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With