--Doraemon => Doraemon
text = u'Doraemon New Nobita's Great Demon'
d = dict([(x, unichr(x - 0x60)) for x in xrange(ord(u'A'), ord(u'Vu')+1)] + [(x - 0x60, unichr(x)) for x in xrange(ord(u'A'), ord(u'Vu')+1)])
print text.translate(d)
Doraemon New Nobita's Great Demon
--Using the fact that the difference between UTF-8 Hiragana and Katakana is 0x60, create a map of Hiragana → Katakana and Katakana → Hiragana. --Translate using str.translate.
I'm only thinking about unicode, so input validation may be necessary.
Recommended Posts