[Python] 高速勉強 #6 「文字列操作その2」

文字列操作は終わりを迎えることはありません。
今回は型の違う文字列を一緒に扱う場合の方法です。
他の言語でも似たような書き方があるので、馴染み深いと思います。
dotinatall
メモ
1 2 3 4 5 6 7 8 9 10 11 12 |
a = 10 b = 1.234234 c = "hoge" d = {"aa":100 , "bb":200} print "1.int: %d" % a //整数値 print "2.int: %10d" % a //10桁分の表示 print "3.int: %010d" % a //10桁分で空欄の箇所を「0」で埋める print "4.float: %f" % b //小数値 print "5.float: %.2f" % b //小数点2桁まで print "6.name: %s" % c //文字列 print "7.test: %(aa)d" % d //辞書型 print "8.%d and %f" % (a , b) //複数の値を入れ込む場合 |
結果
1 2 3 4 5 6 7 8 |
1.int: 10 2.int: 10 3.int: 0000000010 4.float: 1.234234 5.float: 1.23 6.name: hoge 7.test: 200 8.10 and 1.234234 |
使い方
WEBシステムなどのレポート表示の際に、計算結果後の数値を画面の表の中に表示させるような場合に、テンプレートに対して、フォーマット化された値を表示するような場合に利用できます。