Open Source Web Development Tutorials - Dev Shed
Python:文字列とご一緒に
(2008/11/05公開)
Pythonプログラミングの紹介(2008/10/01公開)
Pythonの入力と変数を攻略しよう!(2008/10/08公開)
Pythonの条件文とリストと辞書と演算子を一気に攻略!(2008/10/15公開)
Pythonの集合、ギャングも集合!(2008/10/22公開)
Pythonの演算子、とりあえずビールで!(2008/10/29公開)
文字列とご一緒に
もし読者がプログラムの経験をお持ちであるか、あるいは90歳の女性か、またはネコなら、文字列(string=ひもの意)の便利さにお気づきだろう。このシリーズをお読みのみなさんなら、文字列とは文字、文、段落や書物などのことだとご存じのことだろう。お読みでない方は残念ながらプログラマー教育の重要な部分が欠落していることになるので、早速読みはじめていただきたい。
今回の記事ではPythonでの文字列オブジェクトのさまざまな操作方法をご紹介しよう。それにはソートやインデックスの方法や、内蔵されている文字列メソッドなどが含まれる。さあ、その編み物道具を片付け、ネコをけ飛ばし、オリビア・ニュートン=ジョンみたいにフィジカルに体を使おう!
基本
ここでは文字列の基本について考える。復習が必要な読者はこの部分から読んでいただきたい。でももし文字列を書くことに関してはプロだという方は、この部分を飛ばしていただいても、読んだ上であまりにもつまらないと抗議のメールを筆者に送っていただいてもどちらでもいい。
文字列を表示する
#!/usr/local/bin/python
print "Here are some samples of basic strings:"
print " "
print "Now you know and knowing is half the battle"
print "A"
print "9"
print "%#$@!"
print "It was the best of times, it was the worst of times,"
print "It was the age of wisdom, it was the age of foolishness"
print "It was the epoch of belief, it was the epoch of incredulity"
print "It was the season of Light, it was the season of Darkness"
print "This quote goes on forever..."
変数に文字列を保存し、表示する
#!/usr/local/bin/python
apple = "Fudgebuckets"
sentence="Look at this! It's a sentence! No wait...it's two sentences!"
print apple
print sentence
引用文を表示する
#!/usr/local/bin/python
print "And he said to me, 'I pity the fool!' "
print 'And I quote: "Quit yo jibba-jabba!"'
print " "
print "Notice that it is best to use single quotes when you want to do double quotes"
print "And double quotes when you want to do single quotes"
Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.








