Python maketrans()方法
Python maketrans()方法
Python maketrans() 方法用于創(chuàng)建字符映射的轉(zhuǎn)換表,對于接受兩個參數(shù)的最簡單的調(diào)用方式,第一個參數(shù)是字符串,表示需要轉(zhuǎn)換的字符,第二個參數(shù)也是字符串表示轉(zhuǎn)換的目標(biāo)。
注:兩個字符串的長度必須相同,為一一對應(yīng)的關(guān)系。
語法
maketrans()方法語法:
str.maketrans(intab, outtab)
參數(shù)
- intab -- 字符串中要替代的字符組成的字符串。
- outtab -- 相應(yīng)的映射字符的字符串。
返回值
返回字符串轉(zhuǎn)換后生成的新字符串。
實例
以下實例展示了使用maketrans() 方法將所有元音字母轉(zhuǎn)換為指定的數(shù)字:
#!/usr/bin/python # -*- coding: UTF-8 -*- from string import maketrans # 必須調(diào)用 maketrans 函數(shù)。 intab = "aeiou" outtab = "12345" trantab = maketrans(intab, outtab) str = "this is string example....wow/b64/"; print str.translate(trantab);
以上實例輸出結(jié)果如下:
th3s 3s str3ng 2x1mpl2....w4w/b64/
相關(guān)文章
- Python 變量類型
- Python 字典 Dictionary
- Python SMTP發(fā)送郵件
- Python 列表
- Python 集合
- Python3 注釋
- Python3 數(shù)字(Number)
- Python3 網(wǎng)絡(luò)編程
- Python modf() 函數(shù)
- Python os.ftruncate() 方法
- Python os.mkdir() 方法
- Python os.readlink() 方法
- Python os.rename() 方法
- Python os.statvfs() 方法
- Python isalpha()方法
- Python splitlines()方法
- Python List cmp()方法
- Python 字典 Dictionary cmp()方法
- Python 字典 Dictionary update()方法
- Python time tzset()方法