*Memos:
- My post explains a string.
maketrans() can make the table to translate a string with translate() as shown below:
*Memos:
- The 1st argument is
x
(Required-Type:dict{str/int:str/int/None}
orstr
): *Memos:- It must be
dict
if only one argument is set, which is recommended: *Memos: -
str
keys must be the length 1. -
str
keys are converted to Unicode numbers. - Empty string and
None
values means nothing. - It can be an empty dictionary.
- It must be
str
if two or three arguments are set. - Don't use
x=
.
- It must be
- The 2nd argument is
y
(Optional or Required-Type:str
): *Memos:- It mustn't be set if
x
isdict
. - It must be set and its length must be the same as
x
ifx
isstr
. - Don't use
y=
.
- It mustn't be set if
- The 3rd argument is
z
(Optional-Type:str
): *Memos:- Don't use
z=
.
- Don't use
<maketrans() with one argument>
table1 = str.maketrans({
'a': 'x',
'b': 'y',
'c': 'z',
'1':'one',
'2':'two',
'3':'three',
' ': ' ',
'd': '',
'e': '',
'f': '',
'g': '',
'h': ''
})
# The below is equivalent to the above.
table2 = str.maketrans({
97: 120, # 97('a') & 120('x')
98: 121, # 98('b') & 121('y')
99: 122, # 99('c') & 122('z')
49: 'one', # 49('1')
50: 'two', # 50('2')
51: 'three', # 51('3')
32: ' ', # 32(' ')
100: None, # 100('d')
101: None, # 101('e')
102: None, # 102('f')
103: None, # 103('g')
104: None # 104('h')
})
print(table1)
# {97: 'x', 98: 'y', 99: 'z', 49: 'one', 50: 'two', 51: 'three',
# 32: ' ', 100: '', 101: '', 102: '', 103: '', 104: ''}
print(table2)
# {97: 120, 98: 121, 99: 122, 49: 'one', 50: 'two', 51: 'three',
# 32: ' ', 100: None, 101: None, 102: None, 103: None, 104: None}
table = str.maketrans({
'abc': '',
'': 'abc'
})
# ValueError: string keys in translate table must be of length 1
<maketrans() with two arguments>
table1 = str.maketrans('abc', 'xyz')
table2 = str.maketrans('', '')
print(table1)
# {97: 120, 98: 121, 99: 122}
print(table2)
# {}
table1 = str.maketrans('abc', 'x')
table2 = str.maketrans('x', 'abc')
# ValueError: the first two maketrans arguments must have equal length
<maketrans() with three arguments>
table1 = str.maketrans('abc', 'xyz', 'defgh')
table2 = str.maketrans('', '', '')
print(table1)
# {97: 120, 98: 121, 99: 122, 100: None,
# 101: None, 102: None, 103: None, 104: None}
print(table2)
# {}
table1 = str.maketrans('abc', 'x', 'defgh')
table2 = str.maketrans('x', 'abc', 'defgh')
# ValueError: the first two maketrans arguments must have equal length
translate() can translate a string with the dictionary created by maketrans() as shown below:
*Memos:
- The 1st argument is
table
(Required-Type:dict
): *Memos:- A dictionary should be created with
maketrans()
. - Don't use
table=
.
- A dictionary should be created with
<maketrans() with one argument>
table = str.maketrans({
'a': 'x',
'b': 'y',
'c': 'z',
'1':'one',
'2':'two',
'3':'three',
' ': ' ',
'd': '',
'e': '',
'f': '',
'g': '',
'h': ''
})
print('AaBbCc'.translate(table)) # AxByCz
print('1 2 3'.translate(table)) # one two three
print('DdEeFfGgHh'.translate(table)) # DEFGH
print(''.translate(table))
print(' '.translate(table))
# Nothing
*The below is equivalent to the above.
table = {
'a': 'x',
'b': 'y',
'c': 'z',
'1':'one',
'2':'two',
'3':'three',
' ': ' ',
'd': '',
'e': '',
'f': '',
'g': '',
'h': ''
}
print(''.join(table.get(c, c) for c in 'AaBbCc')) # AxByCz
print(''.join(table.get(c, c) for c in '1 2 3')) # one two three
print(''.join(table.get(c, c) for c in 'DdEeFfGgHh')) # DEFGH
print(''.join(table.get(c, c) for c in ''))
print(''.join(table.get(c, c) for c in ' '))
# Nothing
<maketrans() with two arguments>
table = str.maketrans('abc', 'xyz')
print('AaBbCc'.translate(table)) # AxByCz
print('DdEeFfGgHh'.translate(table)) # DdEeFfGgHh
print(''.translate(table))
print(' '.translate(table))
# Nothing
table = str.maketrans('', '')
print('AaBbCc'.translate(table)) # AaBbCc
print('DdEeFfGgHh'.translate(table)) # DdEeFfGgHh
print(''.translate(table))
print(' '.translate(table))
# Nothing
<maketrans() with three arguments>
table = str.maketrans('abc', 'xyz', 'defgh')
print('AaBbCc'.translate(table)) # AxByCz
print('DdEeFfGgHh'.translate(table)) # DEFGH
print(''.translate(table))
print(' '.translate(table))
# Nothing
table = str.maketrans('', '', '')
print('AaBbCc'.translate(table)) # AaBbCc
print('DdEeFfGgHh'.translate(table)) # DdEeFfGgHh
print(''.translate(table))
print(' '.translate(table))
# Nothing
Top comments (0)