Python の組み込み関数 ord の使い方です。
ord 関数で、1 つの文字 (str) から Unicode コードポイントの数値を取得するコード例と、実行結果を載せました。
ord 関数は、1 つの文字を、Unicode コードポイントの数値に変換してくれました。
(例)ord('a') -> 97
(例)ord('あ') -> 12354
あと、『エラーになったコード例』も書きました。
エラーになったコード例
ord 関数で、エラーになったコード例です。
ord('abc') # 'abc'
TypeError: ord() expected a character, but string of length 3 found
ord(1) # 1
TypeError: ord() expected string of length 1, but int found
ord(1.2) # 1.2
TypeError: ord() expected string of length 1, but float found
ord(complex(3, 4)) # (3+4j)
TypeError: ord() expected string of length 1, but complex found
ord(None) # None
TypeError: ord() expected string of length 1, but NoneType found
ord(True) # True
TypeError: ord() expected string of length 1, but bool found
ord(False) # False
TypeError: ord() expected string of length 1, but bool found
ord(()) # () 空のタプル
TypeError: ord() expected string of length 1, but tuple found
ord([]) # [] 空のリスト
TypeError: ord() expected string of length 1, but list found
ord(set()) # set() 空の集合
TypeError: ord() expected string of length 1, but set found
ord({}) # {} 空の辞書
TypeError: ord() expected string of length 1, but dict found
ord(range(0, 1)) # range(0, 1)
TypeError: ord() expected string of length 1, but range found
ord(lambda x: x) # <function <lambda> at 0x0000000000000000>
TypeError: ord() expected string of length 1, but function found
import datetime
ord(datetime.datetime.max) # datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
TypeError: ord() expected string of length 1, but datetime.datetime found
import datetime
ord(datetime.timedelta.max) # datetime.timedelta(days=999999999, seconds=86399, microseconds=999999)
TypeError: ord() expected string of length 1, but datetime.timedelta found
str を渡した結果
ord 関数に、1 文字の str を渡した結果です。
文字列の中のエスケープシーケンス \x
\u
\U
の意味は、(Python) 文字列およびバイト列リテラル のところに書かれていました。
\xhh
は、2 桁の 16 進数値 hh を持つ文字です。\uxxxx
は、4 桁の 16 進数値 xxxx を持つ文字です。\Uxxxxxxxx
は、8 桁の 16 進数値 xxxxxxxx を持つ文字です。
あと、空の文字列や、長さが 2 以上の文字列を渡したら、エラーになりました。
(Python) 整数リテラル (Integer literals)
ord('') # '' 空文字列
TypeError: ord() expected a character, but string of length 0 found
ord('\x00\x01') # '\x00\x01'
TypeError: ord() expected a character, but string of length 2 found
ord('\x00') # '\x00'
0
ord('\x01') # '\x01'
1
ord('\x02') # '\x02'
2
ord('\x40') # '@'
64
ord('@') # '@'
64
ord('\x41') # 'A'
65
ord('A') # 'A'
65
ord('\x42') # 'B'
66
ord('B') # 'B'
66
ord('\xfe') # 'þ'
254
ord('þ') # 'þ'
254
ord('\xff') # 'ÿ'
255
ord('ÿ') # 'ÿ'
255
ord('\u0100') # 'Ā'
256
ord('Ā') # 'Ā'
256
ord('\u3042') # 'あ'
12354
ord('あ') # 'あ'
12354
ord('\U0010fffe') # '\U0010fffe'
1114110
ord('\U0010ffff') # '\U0010ffff'
1114111
str 型での 1 文字の表現についてです。
Unicode コードポイントが、1114111 (0x10FFFF) よりも大きい文字を指定したら、エラーになりました。
str の 1 文字でいうと、'\U0010ffff'
より大きい表現が、エラーになりました。
'\U0010fffe'
'\U0010fffe'
'\U00110000'
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-9: illegal Unicode character
'\U00110001'
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-9: illegal Unicode character
bytes を渡した結果
ord 関数に、長さが 1 のバイト列 (bytes) を渡した結果です。
ところで、長さが 1 のバイト列で表せる数は、0 (0x00) から 255 (0xff) まででした。
なので、ord 関数に渡せたバイト列も、b'\x00'
から b'\xff'
まででした。
あと、空のバイト列や、長さが 2 以上のバイト列を渡したら、エラーになりました。
(Python) class bytes([source[, encoding[, errors]]])
(Python) 整数リテラル (Integer literals)
ord(b'') # b'' 空のバイト列
TypeError: ord() expected a character, but string of length 0 found
ord(b'\x00\x01') # b'\x00\x01'
TypeError: ord() expected a character, but string of length 2 found
ord(b'\x00') # b'\x00'
0
ord(b'\x01') # b'\x01'
1
ord(b'\x02') # b'\x02'
2
ord(b'\x40') # b'@'
64
ord(b'@') # b'@'
64
ord(b'\x41') # b'A'
65
ord(b'A') # b'A'
65
ord(b'\x42') # b'B'
66
ord(b'B') # b'B'
66
ord(b'\xfe') # b'\xfe'
254
ord(b'\xff') # b'\xff'
255
以上です。