Python文字列覚書
文字列の種類
- バイト文字列
- Unicode文字列
- raw文字列
文字列の扱い方
>>> 'abc', "abc" ('abc', 'abc’) >>> """abc ... def""" 'abc\ndef’ >>> print('abc\ndef') abc def >>> print(r'abc\def') abc\def >>> '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
参考図書
Python入門[2&3対応]
posted with ヨメレバ
細田 謙二,石井 光次郎,岩川 建彦,岡田 正彦,オレンジ岸本,エスキュービズム 秀和システム 2010-06-24