LoginSignup
0
0

More than 3 years have passed since last update.

文字の変換〜translate()関数

Last updated at Posted at 2019-09-18

translate()関数を使用し、文字変換をさせる

python3で、translate()を使用し、"money"表記を一文字変え、"honey"表記に換えてみる

コード例

text = "money"
x = {"m":"h"}

y = text.translate(str.maketrans(x))
print(y)

解釈

text = 'money'
変数に"money"を代入。

x = {'m':'h'}
辞書型で、keyとしての"m"と、valueとしての"n"を変数に代入。

y = text.translate(str.maketrans(x))
"maketrans()"で、translate()関数で使用できる、変換テーブルを作成。
"translate()"関数で、文字列の変換。

最後に

文字の置換は、実業務でも頻繁に使いますね。様々な書き方をマスターしたいです。

0
0
0

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