LoginSignup
0
0

More than 3 years have passed since last update.

平仮名・片仮名の小文字を大文字にするワンライナー

Posted at

概要

translate_large_kana = lambda x: x.translate(dict([(ord(k), ord(k) + 1) for k in ('ぁ', 'ァ', 'ぃ', 'ィ', 'ぅ', 'ゥ', 'ぇ', 'ェ', 'ぉ', 'ォ', 'っ', 'ッ', 'ゃ', 'ャ', 'ゅ', 'ュ', 'ょ', 'ョ')]))
translate_large_kana('シャツ')
# シヤツ

何コレ?

  • ord('ぁ')ord('あ') みたいな小文字と大文字は ord の差が 1 なので、 chr(ord('ぁ') + 1) == 'あ'になる
  • ↑の概念をもとに、小文字を大文字に変換する辞書を作成して、 str.translate する
  • という lambda 関数を作った
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0