Loose-Info.com
Last Update 2025/09/06
TOP - 各種テスト - Python - リテラル

概要

整数リテラル
浮動小数点リテラル
文字列リテラル
バイト列リテラル
接頭辞 r R (文字列・バイト列リテラル)
フォーマット済み文字列リテラル


整数リテラル


sample.py
print("整数リテラル") samp_d1 = 123 # nonzerodigit digit digit samp_d2 = 1_2_3 # nonzerodigit _ digit _ digit samp_d3 = 000 # 0 0 0 samp_d4 = 0_0_0 # 0 _ 0 _ 0 print("123 samp_d1", samp_d1, samp_d1.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("1_2_3 samp_d2", samp_d2, samp_d2.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("000 samp_d3", samp_d3, samp_d3.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0_0_0 samp_d4", samp_d4, samp_d4.to_bytes(2).hex(sep=' '), sep=' --- ' ) samp_b1 = 0b111 # 0 b bindigit bindigit bindigit samp_b2 = 0B111 # 0 B bindigit bindigit bindigit samp_b3 = 0b_111 # 0 b _ bindigit bindigit bindigit samp_b4 = 0b_1_1_1 # 0 b _ bindigit _ bindigit _ bindigit print("0b111 samp_b1", samp_b1, samp_b1.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0B111 samp_b2", samp_b2, samp_b2.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0b_111 samp_b3", samp_b3, samp_b3.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0b_1_1_1 samp_b4", samp_b4, samp_b4.to_bytes(2).hex(sep=' '), sep=' --- ' ) samp_o1 = 0o567 # 0 o octdigit octdigit octdigit samp_o2 = 0O567 # 0 O octdigit octdigit octdigit samp_o3 = 0o_567 # 0 o _ octdigit octdigit octdigit samp_o4 = 0o_5_6_7 # 0 o _ octdigit _ octdigit _ octdigit print("0o567 samp_o1", samp_o1, samp_o1.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0O567 samp_o2", samp_o2, samp_o2.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0o_567 samp_o3", samp_o3, samp_o3.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0o_5_6_7 samp_o4", samp_o4, samp_o4.to_bytes(2).hex(sep=' '), sep=' --- ' ) samp_h1 = 0xDEF # 0 x hexdigit hexdigit hexdigit samp_h2 = 0XDEF # 0 X hexdigit hexdigit hexdigit samp_h3 = 0x_DEF # 0 x _ hexdigit hexdigit hexdigit samp_h4 = 0x_D_E_F # 0 x _ hexdigit _ hexdigit _ hexdigit print("0xDEF samp_h1", samp_h1, samp_h1.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0XDEF samp_h2", samp_h2, samp_h2.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0x_DEF samp_h3", samp_h3, samp_h3.to_bytes(2).hex(sep=' '), sep=' --- ' ) print("0x_D_E_F samp_h4", samp_h4, samp_h4.to_bytes(2).hex(sep=' '), sep=' --- ' )

実行結果
$ python3 sample.py 整数リテラル 123 samp_d1 --- 123 --- 00 7b 1_2_3 samp_d2 --- 123 --- 00 7b 000 samp_d3 --- 0 --- 00 00 0_0_0 samp_d4 --- 0 --- 00 00 0b111 samp_b1 --- 7 --- 00 07 0B111 samp_b2 --- 7 --- 00 07 0b_111 samp_b3 --- 7 --- 00 07 0b_1_1_1 samp_b4 --- 7 --- 00 07 0o567 samp_o1 --- 375 --- 01 77 0O567 samp_o2 --- 375 --- 01 77 0o_567 samp_o3 --- 375 --- 01 77 0o_5_6_7 samp_o4 --- 375 --- 01 77 0xDEF samp_h1 --- 3567 --- 0d ef 0XDEF samp_h2 --- 3567 --- 0d ef 0x_DEF samp_h3 --- 3567 --- 0d ef 0x_D_E_F samp_h4 --- 3567 --- 0d ef

浮動小数点リテラル


sample.py
print("浮動小数点リテラル") print("pointfloat") samp_f1 = .123 # fraction ---> . digit digit digit samp_f2 = .1_2_3 # fraction ---> . digit _ digit _ digit samp_f3 = 123. # digitpart "." ---> digit digit digit . samp_f4 = 1_2_3. # digitpart "." ---> digit _ digit _ digit . samp_f5 = 0.123 # digitpart fraction ---> digit . digit digit digit samp_f6 = 123.123 # digitpart fraction ---> digit digit digit . digit digit digit print(".123 samp_f1", samp_f1) print(".1_2_3 samp_f2", samp_f2) print("123. samp_f3", samp_f3) print("1_2_3. samp_f4", samp_f4) print("0.123 samp_f5", samp_f5) print("123.123 samp_f6", samp_f6) print("exponentfloat") samp_e1 = 1.23e-1 # pointfloat exponent ---> digit . digit digit e - digit samp_e2 = 123123E-3 # digitpart exponent ---> digit digit digit digit digit digit E - digit samp_e3 = 0.1_2_3e+3 # pointfloat exponent ---> digit . digit _ digit _ digit e + digit print("1.23e-1 samp_e1", samp_e1) print("123123E-3 samp_e2", samp_e2) print("0.1_2_3e+3 samp_e3", samp_e3)

実行結果
$ python3 sample.py 浮動小数点リテラル pointfloat .123 samp_f1 0.123 .1_2_3 samp_f2 0.123 123. samp_f3 123.0 1_2_3. samp_f4 123.0 0.123 samp_f5 0.123 123.123 samp_f6 123.123 exponentfloat 1.23e-1 samp_e1 0.123 123123E-3 samp_e2 123.123 0.1_2_3e+3 samp_e3 123.0

文字列リテラル


sample.py
print("文字列リテラル(接頭辞無し)") print("---------- shortstring ----------") samp_s1 = 'aあ\nb' # ' shortstringchar shortstringchar stringescapeseq shortstringchar ' samp_s2 = "aあ\nb" # " shortstringchar shortstringchar stringescapeseq shortstringchar " print("samp_s1 =", samp_s1) print("samp_s2 =", samp_s2) print("---------- longstring ----------") samp_s3 = ''' ab あ "cd" 'ef' g\n h ''' # ''' # longstringchar longstringchar longstringchar(newline) # longstringchar longstringchar(newline) # " longstringchar longstringchar " longstringchar(newline) # ' longstringchar longstringchar ' longstringchar(newline) # longstringchar stringescapeseq longstringchar(newline) # longstringchar longstringchar(newline) # ''' samp_s4 = """ ab あ "cd" 'ef' g\n h """ # """ # longstringchar longstringchar longstringchar(newline) # longstringchar longstringchar(newline) # " longstringchar longstringchar " longstringchar(newline) # ' longstringchar longstringchar ' longstringchar(newline) # longstringchar stringescapeseq longstringchar(newline) # longstringchar longstringchar(newline) # """ print("samp_s3 =", samp_s3) print("samp_s4 =", samp_s4)

実行結果
$ python3 sample.py 文字列リテラル(接頭辞無し) ---------- shortstring ---------- samp_s1 = aあ b samp_s2 = aあ b ---------- longstring ---------- samp_s3 = ab あ "cd" 'ef' g h samp_s4 = ab あ "cd" 'ef' g h

バイト列リテラル


sample.py
print("バイト列リテラル(接頭辞b(B))") print("---------- shortbytes ----------") samp_b1 = b'ab\nc' # b ' shortbyteschar shortbyteschar bytesescapeseq shortbyteschar ' samp_b2 = b"ab\nc" # b " shortbyteschar shortbyteschar bytesescapeseq shortbyteschar " samp_b3 = B'ab\nc' # B ' shortbyteschar shortbyteschar bytesescapeseq shortbyteschar ' samp_b4 = B"ab\nc" # B " shortbyteschar shortbyteschar bytesescapeseq shortbyteschar " print("samp_b1 =", samp_b1, "---", samp_b1.hex(sep=' '), "---", samp_b1.decode()) print("samp_b2 =", samp_b1, "---", samp_b2.hex(sep=' '), "---", samp_b1.decode()) print("samp_b3 =", samp_b1, "---", samp_b3.hex(sep=' '), "---", samp_b1.decode()) print("samp_b4 =", samp_b1, "---", samp_b4.hex(sep=' '), "---", samp_b1.decode()) print("---------- longbytes ----------") samp_b5 = b'''ab "cd" 'ef' g\n h''' # b ''' longbyteschar longbyteschar longbyteschar(newline) # " longbyteschar longbyteschar " longbyteschar(newline) # ' longbyteschar longbyteschar ' longbyteschar(newline) # longbyteschar bytesescapeseq longbyteschar(newline) # longbyteschar ''' samp_b6 = B"""ab "cd" 'ef' g\n h""" # b """ longbyteschar longbyteschar longbyteschar(newline) # " longbyteschar longbyteschar " longbyteschar(newline) # ' longbyteschar longbyteschar ' longbyteschar(newline) # longbyteschar bytesescapeseq longbyteschar(newline) # longbyteschar """ print("samp_b5 =", samp_b5, "---", samp_b5.hex(sep=' '), "---", samp_b5.decode()) print("samp_b6 =", samp_b6, "---", samp_b6.hex(sep=' '), "---", samp_b6.decode())

実行結果
$ python3 sample.py バイト列リテラル(接頭辞b(B)) ---------- shortbytes ---------- samp_b1 = b'ab\nc' --- 61 62 0a 63 --- ab c samp_b2 = b'ab\nc' --- 61 62 0a 63 --- ab c samp_b3 = b'ab\nc' --- 61 62 0a 63 --- ab c samp_b4 = b'ab\nc' --- 61 62 0a 63 --- ab c ---------- longbytes ---------- samp_b5 = b'ab\n"cd"\n\'ef\'\ng\n\nh' --- 61 62 0a 22 63 64 22 0a 27 65 66 27 0a 67 0a 0a 68 --- ab "cd" 'ef' g h samp_b6 = b'ab\n"cd"\n\'ef\'\ng\n\nh' --- 61 62 0a 22 63 64 22 0a 27 65 66 27 0a 67 0a 0a 68 --- ab "cd" 'ef' g h

接頭辞 r R (文字列・バイト列リテラル)


sample.py
print("文字列リテラル・バイト列リテラル(接頭辞 r R)") print("二重引用符での試行は省略") print("---------- shortstring ----------") samp_s1 = r'ab\nc' samp_s2 = R'ab\nc' print("samp_s1 =", samp_s1) print("samp_s2 =", samp_s2) print("---------- shortbytes ----------") samp_b1 = br'ab\nc' samp_b2 = bR'ab\nc' print("samp_b1 =", samp_b1) print("samp_b2 =", samp_b2) print("---------- longstring ----------") samp_s3 = r''' ab "cd" 'ef' g\n h ''' print("samp_s3 =", samp_s3) print("---------- longbytes ----------") samp_b3 = bR''' ab "cd" 'ef' g\n h ''' print("samp_b3 =", samp_b3)

実行結果
$ python3 sample.py 文字列リテラル・バイト列リテラル(接頭辞 r R) 二重引用符での試行は省略 ---------- shortstring ---------- samp_s1 = ab\nc samp_s2 = ab\nc ---------- shortbytes ---------- samp_b1 = b'ab\\nc' samp_b2 = b'ab\\nc' ---------- longstring ---------- samp_s3 = ab "cd" 'ef' g\n h ---------- longbytes ---------- samp_b3 = b'\nab\n"cd"\n\'ef\'\ng\\n\nh\n'

フォーマット済み文字列リテラル


sample.py
class TestClass: def __init__(self): self.samp1 = 1 self.samp2 = "abc" print("フォーマット済み文字列リテラル") s1 = "abc" s2 = "あ" print(f"d{s1}e") print(f"{s1=}de") print(f"d{s1!s}e") print(f"d{s1!r}e") print(f"d{s1!a}e") print(f"d{s2!s}e") print(f"d{s2!r}e") print(f"d{s2!a}e") print(f"{s1=} {s2=}") n = 0 print(f"n == 0 : {'yes' if n == 0 else 'no '} : n=0") n = 1 print(f"n == 0 : {'yes' if n == 0 else 'no '} : n=1") tc = TestClass() print(f"tc.samp1 = {tc.samp1} : tc.samp2 = {tc.samp2}") print(f"print(tc.samp2) = print({tc.samp2!r})") print(f"tc.samp1 = {tc.samp1:04d}") s3 = ( f"{s1}\n" f"{s2}\n" f"{n:05d}" ) print(f"s3 = {s3}") print(f"{s1}") print(f"{{s1}}") print(f"{{{s1}}}") print(f"{'3'*10}")

実行結果
$ python3 sample.py フォーマット済み文字列リテラル dabce s1='abc'de dabce d'abc'e d'abc'e dあe d'あ'e d'\u3042'e s1='abc' s2='あ' n == 0 : yes : n=0 n == 0 : no : n=1 tc.samp1 = 1 : tc.samp2 = abc print(tc.samp2) = print('abc') tc.samp1 = 0001 s3 = abc あ 00001 abc {s1} {abc} 3333333333

実行環境

Python 3.12.11


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。