開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)のまとめ演習9(不変性オブジェクト)を解いてみる。
9.
文字列は不変性オブジェクトだから直接変更はできない。新しい文字列を作成する必要がある。
インデックスによる位置の指定と値の代入ではこのコードは書けない。インデクシング、スライシング、連結を使って元の文字列を利用して新たな文字列を坂雨声して、それを元の文字列の変数に代入することで文字列を変更できる。
対話型コマンドプロンプトで確認。
入出力結果(Terminal)
$ python Python 3.2.3 (default, Apr 18 2012, 20:17:30) [GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> S='spam' >>> S[1]='l File "<stdin>", line 1 S[1]='l ^ SyntaxError: EOL while scanning string literal >>> S=S[0]+'l'+S[2:] >>> S 'slam' >>> quit() $
0 コメント:
コメントを投稿