0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python2系/3系、文字コードとprint文/コマンドライン

Posted at

python2系

宣言

>>> text = 'テキスト'
>>> btext = b'テキスト'
>>> utext = u'テキスト'

出力

>>> text
'\xe3\x83\x86\xe3\x82\xad\xe3\x82\xb9\xe3\x83\x88'

>>> print text
テキスト

>>> btext
'\xe3\x83\x86\xe3\x82\xad\xe3\x82\xb9\xe3\x83\x88'

>>> btext.decode('utf-8')
u'\u30c6\u30ad\u30b9\u30c8'

>>> print btext.decode('utf-8')
テキスト

>>> print btext
テキスト

>>> utext
u'\u30c6\u30ad\u30b9\u30c8'

>>> print utext
テキスト

python3系

宣言

>>> text = 'テキスト'
>>> btext = b'テキスト'
  File "<stdin>", line 1
SyntaxError: bytes can only contain ASCII literal characters.

>>> utext = u'テキスト'

出力

>>> text
'テキスト'

>>> print(text)
テキスト

>>> utext
'テキスト'

>>> print(utext)
テキスト
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?