DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

String in Python (13)

Buy Me a Coffee

*Memos:

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} or str): *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=.
  • The 2nd argument is y(Optional or Required-Type:str): *Memos:
    • It mustn't be set if x is dict.
    • It must be set and its length must be the same as x if x is str.
    • Don't use y=.
  • The 3rd argument is z(Optional-Type:str): *Memos:
    • Don't use z=.

<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}
Enter fullscreen mode Exit fullscreen mode
table = str.maketrans({
 'abc': '',
    '': 'abc'
})
# ValueError: string keys in translate table must be of length 1
Enter fullscreen mode Exit fullscreen mode

<maketrans() with two arguments>

table1 = str.maketrans('abc', 'xyz')
table2 = str.maketrans('', '')

print(table1)
# {97: 120, 98: 121, 99: 122}

print(table2)
# {}
Enter fullscreen mode Exit fullscreen mode
table1 = str.maketrans('abc', 'x')
table2 = str.maketrans('x', 'abc')
# ValueError: the first two maketrans arguments must have equal length
Enter fullscreen mode Exit fullscreen mode

<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)
# {}
Enter fullscreen mode Exit fullscreen mode
table1 = str.maketrans('abc', 'x', 'defgh')
table2 = str.maketrans('x', 'abc', 'defgh')
# ValueError: the first two maketrans arguments must have equal length
Enter fullscreen mode Exit fullscreen mode

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=.

<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
Enter fullscreen mode Exit fullscreen mode

*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
Enter fullscreen mode Exit fullscreen mode

<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
Enter fullscreen mode Exit fullscreen mode
table = str.maketrans('', '')

print('AaBbCc'.translate(table))     # AaBbCc
print('DdEeFfGgHh'.translate(table)) # DdEeFfGgHh

print(''.translate(table))
print(' '.translate(table))
# Nothing
Enter fullscreen mode Exit fullscreen mode

<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
Enter fullscreen mode Exit fullscreen mode
table = str.maketrans('', '', '')

print('AaBbCc'.translate(table))     # AaBbCc
print('DdEeFfGgHh'.translate(table)) # DdEeFfGgHh

print(''.translate(table))
print(' '.translate(table))
# Nothing
Enter fullscreen mode Exit fullscreen mode

Top comments (0)