TEDIA会員に登録したメールアドレスとパスワードを入力してください

メールアドレス:

     パスワード:


パスワードを忘れた方はパスワードの確認を行ってください。

TEDIA会員へのご登録がお済みで無い方はこちらで登録ができます


>> テクノロジーポータル TEDIA トップページへ戻る <<

mark Software Developer's Think IT mark 失敗しないソフト選び Find-IT mark テクノロジーポータル TEDIA

会員登録 登録情報管理

TEDIA SponsorsOpen Source Web Development Tutorials - Dev Shed

Python:文字列とご一緒に
(2008/11/05公開)

文字列とご一緒に

 もし読者がプログラムの経験をお持ちであるか、あるいは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"


1    2    3    4    次のページ

Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.