開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のII部(ビルトインオブジェクト)、7章(文字列)の練習問題を解いてみる。
その他参考書籍
1, 2, 3.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- # 文字列のメソッドfind(findに限らず文字列メソッド)はリストの検索に仕様できない # スライシングの式はリスト(シーケンス)に使用出来る print([1,2,3,4,5][2:4]) s = "a" # ASCIIコード(整数)に変換 n = ord(s) # 文字に変換 s1 = chr(n) print(s, n, s1) # 文字列はイミュータブル(不変性オブジェクト)なので変更を加える事はできない # 代わりに新しい文字列を作る s = "python" new_s = s + " programmer" s = "s, pa, m" print(s[3:5]) print(s.split(", ")[1]) s = "a\nb\x1f\000d" print(len(s)) # 6文字 a, 改行, b, 16進数, 8進数, d for c in s: print(c, end=" ") print()
入出力結果(Terminal)
$ ./sample.py [3, 4] a 97 a pa pa 6 a b d $
0 コメント:
コメントを投稿