Help us understand the problem. What is going on with this article?

文字の変換〜translate()関数

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()"関数で、文字列の変換。

最後に

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

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away