2014年2月12日水曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART II.(Types and Operations)、CHAPTER 7(String Fundamentals)、Test Your Knowledge: Quiz 1, 2, 3, 4, 5, 6.を解いてみる。

その他参考書籍

Test Your Knowledge: Quiz 1, 2, 3, 4, 5, 6.

1.

文字列でリストのメソッドは使えない。

2.

スライスはシーケンスに使える。そして文字列はリストと同様にシーケンスだからスライスを使える。

3.

文字をASCIIコードの数値にするにはord、その逆、数値を文字に変換するにはchrを使えばいい。

4.

文字列は不変性を持つオブジェクトなので、変更はできない。既存の文字列を変更したい場合には、新しい文字列を作ることになる。

入出力結果(Terminal)

$ python3
Python 3.3.3 (default, Dec  2 2013, 01:40:21) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'python'[1:4]
'yth'
>>> ord('a')
97
>>> chr(97)
'a'
>>> s = 'python'
>>> s[0] = 'j' # エラー
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s = 'j' + s[1:]
>>> s
'jython'
>>> s = 's,pa,m'
>>> s[2:4]
'pa'
>>> s.split(',')[1]
'pa'
>>> s='a\nb\x1f\000d'
>>> len(s) # 6
6
>>> s[0]
'a'
>>> s[1]
'\n'
>>> s[2]
'b'
>>> s[3]
'\x1f'
>>> s[4]
'\x00'
>>> s[5]
'd'
>>> quit()
$

0 コメント:

コメントを投稿