Reputation: 80396
I need convert numeric strings to superscript, is there a better (built-in) way of doing it?
def to_sup(s):
sups={u'1': u'\xb9',
u'0': u'\u2070',
u'3': u'\xb3',
u'2': u'\xb2',
u'5': u'\u2075',
u'4': u'\u2074',
u'7': u'\u2077',
u'6': u'\u2076',
u'9': u'\u2079',
u'8': u'\u2078'}
if s.isdigit():
return ''.join([sups[i] for i in s])
print to_sup('0123')
Output:
⁰¹²³
Upvotes: 6
Views: 4587
Reputation: 97641
Your way is slightly incorrect. Better would be:
def to_sup(s):
sups = {u'0': u'\u2070',
u'1': u'\xb9',
u'2': u'\xb2',
u'3': u'\xb3',
u'4': u'\u2074',
u'5': u'\u2075',
u'6': u'\u2076',
u'7': u'\u2077',
u'8': u'\u2078',
u'9': u'\u2079'}
return ''.join(sups.get(char, char) for char in s) # lose the list comprehension
s.isdigit()
will only check the first letter, which probably doesn't make sense.
If for some reason you want a one-liner:
u''.join(dict(zip(u"0123456789", u"⁰¹²³⁴⁵⁶⁷⁸⁹")).get(c, c) for c in s)
Upvotes: 10