場所を指定しての取り出し
Pythonの文字列は「[]」や「:」を使って好きな場所を切り出すことができます。
testStr = "123456789" print testStr[2] #3文字目のみ print testStr[:2] #前から2文字 print testStr[2:4] #3文字目から4文字目 print testStr[2:] #3文字以降 print testStr[-2:] #最後2文字 print testStr[:-2] #最後2文字以外
結果
3 12 34 3456789 89 1234567
リストでも同様のことができます。つまり文字列自体がリストのようなものになっていて、1文字目がインデックス0となっています。
Unicodeでも同じです。
Page Info | |
---|---|
Page Name : | Python/文字列の場所を指定して切り出す |
Page aliases : | None |
Page owner : | njf |
Can Read | |
Groups : | All visitors |
Users : | All visitors |
Can Edit | |
Groups : | All visitors |
Users : | All visitors |
Counter: 1356,
today: 1,
yesterday: 0
Princeps date: 2016-12-05 (Mon) 13:24:57
Last-modified: 2016-12-05 (Mon) 13:26:40 (JST) (2075d) by njf