Python の組み込み関数 chr の使い方です。
chr 関数で、整数から文字列を取得するコード例と、実行結果を載せました。
chr 関数は、Unicode コードポイントの数値を、文字列 (str) に変換してくれました。
(例)chr(97) -> 'a'
(例)chr(12354) -> 'あ'
あと、『エラーになったコード例』も書きました。
エラーになったコード例
chr 関数で、エラーになったコード例です。
chr('') # '' 空文字列
TypeError: an integer is required (got type str)
chr('5') # '5'
TypeError: an integer is required (got type str)
chr('a') # 'a'
TypeError: an integer is required (got type str)
chr(b'') # b'' 空のバイト列
TypeError: an integer is required (got type bytes)
chr(b'a') # b'a'
TypeError: an integer is required (got type bytes)
chr(1.2) # 1.2
TypeError: integer argument expected, got float
chr(complex(3, 4)) # (3+4j)
TypeError: can't convert complex to int
chr(None) # None
TypeError: an integer is required (got type NoneType)
chr(()) # () 空のタプル
TypeError: an integer is required (got type tuple)
chr([]) # [] 空のリスト
TypeError: an integer is required (got type list)
chr(set()) # set() 空の集合
TypeError: an integer is required (got type set)
chr({}) # {} 空の辞書
TypeError: an integer is required (got type dict)
chr(range(0, 1)) # range(0, 1)
TypeError: an integer is required (got type range)
chr(lambda x: x) # <function <lambda> at 0x0000000000000000>
TypeError: an integer is required (got type function)
import datetime
chr(datetime.datetime.max) # datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
TypeError: an integer is required (got type datetime.datetime)
import datetime
chr(datetime.timedelta.max) # datetime.timedelta(days=999999999, seconds=86399, microseconds=999999)
TypeError: an integer is required (got type datetime.timedelta)
bool を渡した結果
chr 関数に、bool(真理値)を渡した結果です。
chr(False) # False
'\x00'
chr(True) # True
'\x01'
int を渡した結果
chr 関数に、int(整数)を渡した結果です。
Unicode コードポイントの『数値(整数リテラル)』は、16 進数 (0x
)、10 進数、8 進数 (0o
)、2 進数 (0b
) の、どの表現でも、使用することができました。
(Python) 整数リテラル (Integer literals)
16 進数
16 進数 (0x
) を使用した場合です。
chr(-0x1) # -1
ValueError: chr() arg not in range(0x110000)
chr(0x0) # 0
'\x00'
chr(0x1) # 1
'\x01'
chr(0x2) # 2
'\x02'
chr(0x40) # 64
'@'
chr(0x41) # 65
'A'
chr(0x42) # 66
'B'
chr(0xfe) # 254
'þ'
chr(0xff) # 255
'ÿ'
chr(0x100) # 256
'Ā'
chr(0x3042) # 12354
'あ'
chr(0x10fffe) # 1114110
'\U0010fffe'
chr(0x10ffff) # 1114111
'\U0010ffff'
chr(0x110000) # 1114112
ValueError: chr() arg not in range(0x110000)
10 進数
10 進数を使用した場合です。
chr(-1) # -1
ValueError: chr() arg not in range(0x110000)
chr(0) # 0
'\x00'
chr(1) # 1
'\x01'
chr(2) # 2
'\x02'
chr(64) # 64
'@'
chr(65) # 65
'A'
chr(66) # 66
'B'
chr(254) # 254
'þ'
chr(255) # 255
'ÿ'
chr(256) # 256
'Ā'
chr(12354) # 12354
'あ'
chr(1114110) # 1114110
'\U0010fffe'
chr(1114111) # 1114111
'\U0010ffff'
chr(1114112) # 1114112
ValueError: chr() arg not in range(0x110000)
8 進数
8 進数 (0o
) を使用した場合です。
chr(-0o1) # -1
ValueError: chr() arg not in range(0x110000)
chr(0o0) # 0
'\x00'
chr(0o1) # 1
'\x01'
chr(0o2) # 2
'\x02'
chr(0o100) # 64
'@'
chr(0o101) # 65
'A'
chr(0o102) # 66
'B'
chr(0o376) # 254
'þ'
chr(0o377) # 255
'ÿ'
chr(0o400) # 256
'Ā'
chr(0o30102) # 12354
'あ'
chr(0o4177776) # 1114110
'\U0010fffe'
chr(0o4177777) # 1114111
'\U0010ffff'
chr(0o4200000) # 1114112
ValueError: chr() arg not in range(0x110000)
2 進数
2 進数 (0b
) を使用した場合です。
chr(-0b1) # -1
ValueError: chr() arg not in range(0x110000)
chr(0b0) # 0
'\x00'
chr(0b1) # 1
'\x01'
chr(0b10) # 2
'\x02'
chr(0b1000000) # 64
'@'
chr(0b1000001) # 65
'A'
chr(0b1000010) # 66
'B'
chr(0b11111110) # 254
'þ'
chr(0b11111111) # 255
'ÿ'
chr(0b100000000) # 256
'Ā'
chr(0b11000001000010) # 12354
'あ'
chr(0b100001111111111111110) # 1114110
'\U0010fffe'
chr(0b100001111111111111111) # 1114111
'\U0010ffff'
chr(0b100010000000000000000) # 1114112
ValueError: chr() arg not in range(0x110000)
以上です。