4月 7th, 2008at 15:39
Tags: Python
Python2.5の文字列リテラル
#シングルもダブルも意味は同じとなる print 'シングルクウォーテーション' print "ダブルクウォーテーション"print '''トリプルもあるぜ'''print """トリプルもあるぜ"""print '''トリプルもあるぜ''' #トリプルは改行できます print 'ダブルクウォーテーション""を使える'print 'ダブルクウォーテーション\"\"を使える'print "シングルクウォーテーション''を使える"print "シングルクウォーテーション\'\'を使える"print r'Row文字列は\が無視される\n\\'
実行結果
シングルクウォーテーションダブルクウォーテーショントリプルもあるぜトリプルもあるぜトリプルもあるぜダブルクウォーテーション""を使えるダブルクウォーテーション""を使えるシングルクウォーテーション''を使えるシングルクウォーテーション''を使えるRow文字列は\が無視される\n\\
文字操作
print "私は" '宇宙人' "だ" #これだと自動で連結されるprint "私は" + "宇宙人" + "だ"
print "わん" * 3 #3回くりかえし
string = "1234567890"print string[6] #インデクシング(0からはじまる)print string[2:4] #スライシングprint string[1:] #1から末尾print string[:3] #0から3print string[:] #すべて
#注意:インデクシング、スライシングは2バイト文字だと化ける
#文字列フォーマットprint "このまえ%sに乗った" % "フリーフォール"print "このまえ%sと%sと%sに乗った" % ("フリーフォール", "フリーフォール", "フリーフォール")
実行結果
私は宇宙人だ私は宇宙人だわんわんわん7342345678901231234567890このまえフリーフォールに乗ったこのまえフリーフォールとフリーフォールとフリーフォールに乗った
I'm a software engineer who like travel to island in Japanese. Recently I am enjoying agile manager, coach, product owner for my work. The person grows like that.











