キャスト str(integer) 文字列の置換 replace("before","after") 文字列の分割 split("") 文字列を埋める 文字列が指定した長さになるまで指定した文字を文頭から付け足す rjust(int[埋めた後の長さ],""[埋める1文字]) 文字列が指定した長さになるまで"0"を文頭から付け足す zfill(int[埋めた後の長さ]) 文字列の検索 文字列の先頭が文字列strで始まっているか(T/F) startswith("str") 文字列strに文字列dataが含まれているか(T/F) "data" in str 文字列の大文字・小文字変換 小文字へ upper() 大文字へ lower() 文字列の削除 先頭から文字列str削除(str==NULLなら空白を削除) lstrip(str) 末尾から文字列str削除(str==NULLなら空白を削除) rstrip(str) 日付の取得 datetimeモジュールをインポートする。 """ today = datetime.date.today() todaydetail = datetime.datetime.today() #今日の日付 print "----------------------------" print today print todaydetail #今日の日付:それぞれの要素値 print "----------------------------" print today.year print today.month print today.day print todaydetail.year print todaydetail.month print todaydetail.day print todaydetail.hour print todaydetail.minute print todaydetail.second print todaydetail.microsecond #日付のフォーマット print "----------------------------" print today.isoformat() print todaydetail.strftime("%Y/%m/%d %H:%M:%S") """ """ #日付の計算 today = datetime.datetime.today() today = today + datetime.timedelta(days=1) day = datetime.datetime(2013,1,1) #新年までの日数 calc = day - today print calc.days """ タプル:変更不可能なデータセット、()で囲う value = (*,*,*) リスト:変更可能なデータセット、[]で囲う test_list_1 = ["python","-","izm",".","com"] 各要素を表示 for i in test_list_1: print i 要素strの追加 末尾に追加 append("str") 指定の場所iに追加 insert(i,"str") 指定文字列を削除 最初に見つかった指定文字列strを削除 remove("str") 指定したインデックスの文字列を削除(削除した文字列を返す) pop("str") 末尾の文字列を削除(削除した文字列を返す) pop() 指定した文字列strを要素とするインデックスを返す index("str") 指定した文字列strのリスト内での出現回数を返す count("str") 指定した文字列をリスト内からすべて削除 for i in range(list.count("str")) list.remove("str") ディクショナリ:キーと値のセット dict = {"year":"2012","month":"2","day":"28"} 各要素を表示 for i in test_dict_1: print i print test_dict_1[i] 指定したキーの値を取得 get("key")⇒(value/None) get("key","str")⇒(value/str) 値の追加 list["key"]="word" "a"==u"a" "a"+u"あ"==u"aあ" |