文字列の種類

  • バイト文字列
  • Unicode文字列
  • raw文字列

文字列の扱い方

>>> abc, "abc"
(abc, abc’)

>>> """abc
... def"""
abc
def’

>>> print(abc
def)
abc
def

>>> print(rabcdef)
abcdef

>>> ab + c
‘abc

>>> ab * 3
‘ababab

>>> str([1,2,3,4])
[1, 2, 3, 4]

>>> s = abcdef
>>> s[3]
‘d

>>> s[3:5]
de

>>> s[3] = D
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: str object does not support item assignment

>>> %s is now version %d. %(Python, 3)
Python is now version 3.

>>> %(lang)s is No. %(num)d. %{lang: Python, num: 3}
Python is No. 3.

>>> len(u日本語)
3

>>> len(u日本語.encode(utf8))
9

参考図書