Python で文字列の前に B をつける
Muhammad Maisam Abbas
2021年10月22日

このチュートリアルでは、Python の b"
ステートメントについて説明します。
Python で b"
ステートメントを使用する
b"
表記は、Python で bytes
文字列を指定するために使用されます。ASCII 文字を持つ通常の文字列と比較して、bytes
文字列はバイト変数の配列であり、各 16 進要素の値は 0 から 255。
組み込みの encode()
関数を使用して、通常の文字列をバイト
文字列にエンコードすることもできます。以下のプログラムは、encode()
関数を使用して通常の文字列をバイト
文字列にエンコードする方法を示しています。
string = 'This is a string'
print(string.encode())
出力:
b'This is a string'
上記のコードの encode()
関数を使用して、通常の文字列これは文字列
をバイト
文字列形式にエンコードしました。b"
ステートメントを使用して、文字列をバイト
文字列形式にエンコードすることもできます。次のコードスニペットは、その方法を示しています。
string = b'This is a string'
print(string)
出力:
b'This is a string'
ここで、string
変数は通常の文字列ではありません。代わりに、それはバイト
文字列です。
Author: Muhammad Maisam Abbas
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn関連記事 - Python Bytes
- Python バイトを整数に変換する方法
- Python 2 および Python 3 で Int をバイトに変換する方法
- Python で Int をバイナリに変換する方法
- Python 2 および Python 3 でバイトを文字列に変換する方法
- Python で文字列をバイトに変換する方法